+ 1

Code coach: vowel counter

My code outputs 1 for any input that is taken and I'm not able to figure out what error i made, please help Here goes my code: #include <stdio.h> int main() { char vowel[11]={'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U' }; char s[500];int i=0,count=0; scanf("%s",s); while(s[i]!='\0') { for(int j=0;j<11;j++) { if(s[i]==vowel[j]) count++; } i++; } printf("%d",count); return 0; } I also tried by initialising: vowel[11]={"aeiouAEIOU"};

16th Oct 2024, 4:40 AM
Sanjana
Sanjana - avatar
9 Answers
+ 8
The code works okay if the input has no spaces. Instead of: This is the day Output: 1 Enter: Thisistheday Output: 4 Using "%s" in scanf only reads characters up to a space or a newline. It reads the next word as a separate string. To read the whole line you can use this arcane format specifier: "%[^\n]" It says to read everything from the beginning of the line (symbolized by ^) to the newline (\n). Beware that this will leave the newline in the input buffer. You would need to deal with that if expecting multiple input lines. Another way is to use fgets(s, 500, stdin); to read the whole line. That includes the newline, which you might not want, but it does no harm in this case. The vowel array needs to be only 10 characters. The inner loop should test for j<10. As it is, the loop tests the item at 10, which is past the given vowel characters and implicitly filled in as a 0 by the compiler. Final note: Trying {"aeiouAEIOU"} ends up wrongly storing a string pointer in the char array; not the letters.
16th Oct 2024, 6:12 AM
Brian
Brian - avatar
+ 2
Sanjana you are welcome!
16th Oct 2024, 2:41 PM
Brian
Brian - avatar
+ 1
Brian thank you very much!!
16th Oct 2024, 2:24 PM
Sanjana
Sanjana - avatar
+ 1
Guilherme Rodrigues Code Coach is looking for very exact output. If your program prints more than what the task specifies then the result will be graded as wrong. Try removing the extra printf() that is inside the loop (line 33) so that it prints only once, at the end of processing.
17th Jan 2025, 2:48 PM
Brian
Brian - avatar
+ 1
Guilherme Rodrigues it looks like your code is continuing to evolve, as I see some changes. Here is everything that needs correction as of this writing: That wild ouput number is a result of the vowels variable not getting a proper initial value. Set it = 0 in line 23 [note that only i is getting initialized currently]. But there is more: the vowelsList is initialized wrong. Remove the braces {} that surround the string in line 25. The former attempt in line 24 would have worked. Line 25 with braces removed would work, too, and is cleaner than line 24. The index i somehow ended up getting its increment inside the inner loop. Move it out to the outer loop. And all will be well. Well, except for the extraneous garbage in line 39. Delete that, and then it will compile and run just fine.
18th Jan 2025, 4:16 AM
Brian
Brian - avatar
+ 1
Unfortunately there is no syntax in C that lets you declare and initialize multiple variables with a single value assignment. An alternative is to initialize them at run time with a compound assignment statement, like this: int vowels, i; vowels = i = 0:
18th Jan 2025, 7:34 PM
Brian
Brian - avatar
0
Hello, I came to a similar solution for this challenge, but it didn't work. Can you guys help understand why? https://sololearn.com/compiler-playground/cB5YTdDv89BH/?ref=app
17th Jan 2025, 8:56 AM
Guilherme Rodrigues
Guilherme Rodrigues - avatar
0
Hi Brian, thank you for answering me. I forgot to comment that line before sharing the code. While running the code, using 'abecidofu' as input my code out 31868. I still don't know why...
18th Jan 2025, 2:09 AM
Guilherme Rodrigues
Guilherme Rodrigues - avatar
0
Thanks a lot Brian! I didn't know that writing "int vowels, i = 0" would result in only i being initialized as 0, all the time I thought both variables were being initialized as 0.
18th Jan 2025, 10:42 AM
Guilherme Rodrigues
Guilherme Rodrigues - avatar