Question: C Programming Question: Write a C program that prompts the user to enter a line of text (up to 50 characters). It should then convert
C Programming Question:
Write a C program that prompts the user to enter a line of text (up to 50 characters). It should then convert the text entered to uppercase letters and then to lowercase letters.
Your program should use a character array: text[51] to store text. It should use function safer_gets() to prompt the user for the line of text.
Your program must use pointer notation and pointer math (i.e. no array index notation other than in the declaration section).
The dialog with the user must look like:
Enter a line of text (up to 50 characters): ThiS is A LiNe of teXt with UPPER and lower CaSe charactERS
The line of text in uppercase is: THIS IS A LINE OF TEXT WITH UPPER AND LOWER C
The line of text in lowercase is:
this is a line of text with upper and lower case c
Note: The bold text for the dialogue represents the "output" from your program and is shown for clarity only here. It is not required that your output be in this color. (Do not even attempt to try!). Also note that what the user types in is indicated by the bolded black text above. Again, for clarity only. (Notice that text is truncated once the 50 character limit has been reached - this would be handled in function safer_gets).
Hints: Your program requires 2 loops (either for or while). One to convert and display the string in uppercase letters, and the other to convert and display the string in lowercase letters. Your program should use the C standard library functions toupper in one of the loops, and tolower in the other loop, to convert the string. Both of these functions are found in the header file ctype.h. You do not need to convert function safer_gets() to pointer notation.
Really BIG hint:
Use the template below as your guide. Note: An arrow symbol (->) at the beginning of a particular line of code indicates that the line of code needs to be modified for pointer notation.
#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 (" Enter a line of text (up to 50 characters): "); safer_gets(text,50); /* Convert and output the text in uppercase characters. */ /* ---------------------------------------------------- */ printf (" The line of text in uppercase is: "); -> 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 (" The line of text in lowercase is: "); -> i = 0; -> while ( text[i] != '\0') /* could have been: while ( text[i] ) */ -> putchar( tolower(text[i++]) ); /* Add carriage return and pause output */ /* ------------------------------------ */ printf(" "); 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] == ' ') 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] != ' ') while (getchar() != ' '); /* 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 */
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
