How to write a C program that can convert volvols to a given string to omit if it was top and top, if it was lower?

#include <ctype.h> #include <stdio.h> #include <conio.h> int main(void) { char input[50]; char i; int j = 0; printf("Please enter a sentence: "); fgets(input, 50 , stdin); for (j = 0; input[i] != '\0'; j++) if (input[i]=='a'||input[i]=='e'||input[i]=='i'||input[i]=='o'||input[i]=='u') { input[i]=toupper(input[i]); printf("Your new sentence is: %s", input); } else if (input[i]=='A'||input[i]=='E'||input[i]=='I'||input[i]=='O'||input[i]=='U') { input[i]=tolower(input[i]); printf("Your new sentence is: %s", input); } return 0; } 

This is not my homework . I am new to C. I canโ€™t find out the error in my code, I have googled it, but canโ€™t find any useful data that can fix my error. ERROR, I get printf ("Your new sentence:% s", input); ----> this line does not perform any actions, and I appreciate if someone corrects my error.

INPUT - enter a sentence I'm new to c

Desired exit - Your new offer: I Am A BEgInnEr In c

ACTUAL OUTPUT - // empty string //

thanks

+4
source share
4 answers

as in the other answer, you want to use i, not j .... you also want to put {} in yours. Then move your printf ......., you can make it easier as an extended exercise :)

 for (i = 0; input[i] != '\0'; i++) { if (input[i]=='a'||input[i]=='e'||input[i]=='i'||input[i]=='o'||input[i]=='u') { input[i]=toupper(input[i]); } else if (input[i]=='A'||input[i]=='E'||input[i]=='I'||input[i]=='O'||input[i]=='U') { input[i]=tolower(input[i]); } } printf("Your new sentence is: %s", input); return 0; 
+1
source

Your loop is incorrect:

 for (j = 0; input[i] != '\0'; j++) 

Must be:

 for (i = 0; input[i] != '\0'; i++) 

There is no need for the j variable, since you never use it.

+2
source

What is it worth xoring 0x20 will switch the case of the letter, look at the bits of lowercase letters of upper and lower case in the ASCII table. This is also a good time to familiarize yourself with the standard library. The strchr function will allow you to greatly simplify the code:

 for(i=0; str[i]; i++) if(strchr("aeiouAEIOU", str[i])) str[i] ^= 0x20; 
+2
source

Now this is funny. It can only be homework.

 for (i = 0; input[i] != '\0'; i++) 
0
source

Source: https://habr.com/ru/post/1400501/


All Articles