What is wrong with this code? | SoloLearn: Learn to code for FREE!

+2

What is wrong with this code?

Using a do…while() loop, continuously scan for characters (one per line) and print it out afterwards. The loop shall terminate due to either of the following reasons: The inputted character is a vowel The number of inputted characters has already reached 5. It must also be guaranteed that if the number of inputted characters is less than 5, then there must be a vowel from among the inputted characters. (I tried answering this problem with this code here: https://code.sololearn.com/cA7a5A8a19a1/#) But i cant get all test cases correct, only 3/4. What am i doing wrong? Also i cant check what the 4th test case its because its hidden. Help? https://code.sololearn.com/cA7a5A8a19a1/?ref=app

1/18/2021 8:40:50 AM

Mint

9 Answers

New Answer

+2

Try to change your (while count<10) to (while count<5)

+2

run loop upto 4 becz count Start from 0 and till upto 4 #include <stdio.h> #include <string.h> #include <ctype.h> int main(){ char letter; int count=0; do{ scanf("%c", &letter); char c = tolower(letter); if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') { printf("%c",c); break; } printf("%c",c); count++; } while(count<4); return 0; }

+1

Mint What about while(count < 7) ? This should get 5 inputs.

+1

If you're giving input with single letter per line seperated by newline . Use scanf("%c\n", letter). And in while loop check use 5 instead of 10 While (count!=5) it will exit after count reaches 5. I also have checked and confirmed here. https://code.sololearn.com/cXP8a1WSKtL8/?ref=app Hope this helps .

0

《 Nicko12 》 Doesn't work. Its restricting me to only 3 inputs now

0

♨️♨️ Nope. Its only allowing me 2 character inputs

0

《 Nicko12 》 Just tried. Only 4 inputs

0

Is there input is accepted like "abcde" or line by line like a b C D E For 1st order use scanf("%c", &letter); and while(count<5); For 2nd order type, use : scanf("%c\n", &letter); and while(count<5); 1st one, reads charecter by charecter only while 2nd one reads new line also and I gores it then reads next charecter. Hope it helps...

0

#include <stdio.h> #include <string.h> #include <ctype.h> int main(){ char letter,c; int count=0; do{ scanf("%c", &letter); c = tolower(letter); if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') { printf("First Vowel is"); break; } count++; } while(count<5); printf("%c",c); return 0; }