215k views
5 votes
Write a program to convert a string (of up to 20 characters, including the terminating null character) to all uppercase characters. After getting a string that could include spaces (use the fgets() function and don't forget to strip off the ending if it is there), display the original string. Pass the string to a void function (since the 'string' is really as character array) and have the function convert the string to all uppercase by iterating through the string using a for loop and the strlen() function (which is part of the string.h library), and using the toupper() function (which is part of the ctype.h library) to convert each character in the string. Then, back in the main program, display the all uppercase string.

User Limey
by
4.9k points

1 Answer

7 votes

Answer:

Check the explanation

Step-by-step explanation:

#include <stdio.h>

#include<ctype.h>

#include<string.h>

void MyToUpper(char *p)

{

int i;

for(i=0;i<strlen(p);i++)

{

p[i]=toupper(p[i]);

}

}

int main(void) {

char str[20];

printf("Enter a string:");

fgets(str,20,stdin);

printf("The string you entered was: %s\\",str);

MyToUpper(str);

printf("The string converted to uppercase is: %s\\",str);

fflush(stdin);

return 0;

}

User Kccqzy
by
5.2k points