/* ---------------------------------------------------------------------------------------------- Program : uprlwr.c Course : 90.212 Assignment : Assignment #4 - Array Notation Objective : This program prompts user to enter a line of text up to 50 characters. It then outputs the line of text in uppercase and then in lowercase. ------------------------------------------------------------------------------------------------- */ #include #include /* Function prototypes */ /* ------------------- */ void safer_gets (char array[], int max_chars); /* Begin function main */ /* ------------------- */ main() { /* Declare variables */ /* ----------------- */ char text[51]; int i; /* Prompt user for line of text */ /* ---------------------------- */ printf ("\nEnter a line of text (up to 50 characters):\n"); safer_gets(text,50); /* Convert and output the text in uppercase characters. */ /* ---------------------------------------------------- */ printf ("\nThe line of text in uppercase is:\n"); -> i = 0; -> while ( text[i] != '\0') /* could have been: while ( text[i] ) */ -> putchar( toupper(text[i++]) ); /* Convert and output the text in uppercase characters. */ /* ---------------------------------------------------- */ printf ("\n\nThe line of text in lowercase is:\n"); -> i = 0; -> while ( text[i] != '\0') /* could have been: while ( text[i] ) */ -> putchar( tolower(text[i++]) ); /* Add carriage return and pause output */ /* ------------------------------------ */ printf("\n"); getchar(); } /* end main */ /* Function safer_gets */ /* ------------------- */ void safer_gets (char array[], int max_chars) { /* Declare variables. */ /* ------------------ */ int i; /* Read info from input buffer, character by character, */ /* up until the maximum number of possible characters. */ /* ------------------------------------------------------ */ for (i = 0; i < max_chars; i++) { array[i] = getchar(); /* If "this" character is the carriage return, exit loop */ /* ----------------------------------------------------- */ if (array[i] == '\n') break; } /* end for */ /* If we have pulled out the most we can based on the size of array, */ /* and, if there are more chars in the input buffer, */ /* clear out the remaining chars in the buffer. */ /* This is equivalent to the fflush() function. */ /* ---------------------------------------------------------------- */ if (i == max_chars ) if (array[i] != '\n') while (getchar() != '\n'); /* At this point, i is pointing to the element after the last character */ /* in the string. Terminate the string with the null terminator. */ /* -------------------------------------------------------------------- */ array[i] = '\0'; } /* end safer_gets */