Why Is My C Program Not Counting Vowels Correctly?

  • Thread starter Thread starter chmate
  • Start date Start date
  • Tags Tags
    Counting Program
Click For Summary
The discussion revolves around a C program designed to count vowels in a given text, which is not functioning correctly. The primary issues identified include a logical error in the vowel-checking condition, where the code incorrectly checks if a character is simultaneously equal to multiple vowels, which is impossible. Additionally, the original code does not keep track of the number of vowels counted, and the output message is incorrectly placed within the loop. A suggested solution includes using a switch statement to count both lowercase and uppercase vowels, storing the count in a separate variable, and printing the result after the loop concludes. This approach ensures that the program accurately counts all vowels, including capital letters.
chmate
Messages
37
Reaction score
0
Hi guys!

I made a program in C, which count vowels on text, but it doesn't work.
Thats the code i have writed:

Code:
#include <stdio.h>
#include <string.h>

int main()
{
  char buffer[80];
  int counter;

  printf("Enter a line of text: ");
  fgets(buffer, sizeof(buffer), stdin);
   
  for(counter=0; buffer[counter]!='\0'; counter++)
    {
      if(buffer[counter]=='a' && buffer[counter]=='e'
	 && buffer[counter]=='i' && buffer[counter]=='o'
         && buffer[counter]=='u') 
	continue;
    
      printf("In text, we have %d vowels", strlen(buffer[counter]));
      return 1;	
   }
   return 0;
}

Does anybody have an idea where is the problem?

Thanks.
 
Technology news on Phys.org
chmate said:
Hi guys!

I made a program in C, which count vowels on text, but it doesn't work.
Thats the code i have writed:

Code:
#include <stdio.h>
#include <string.h>

int main()
{
  char buffer[80];
  int counter;

  printf("Enter a line of text: ");
  fgets(buffer, sizeof(buffer), stdin);
   
  for(counter=0; buffer[counter]!='\0'; counter++)
    {
      if(buffer[counter]=='a' && buffer[counter]=='e'
	 && buffer[counter]=='i' && buffer[counter]=='o'
         && buffer[counter]=='u') 
	continue;
    
      printf("In text, we have %d vowels", strlen(buffer[counter]));
      return 1;	
   }
   return 0;
}

Does anybody have an idea where is the problem?

Thanks.
Well you are testing if a character from the buffer is 'a' AND 'e' AND 'i' AND 'o' AND 'u'. Of course that is impossible since it can only have one value.
Second where do you keep track of the number of vowels? You can't use counter for that since it is used to traverse the string. A faster way by the way would be to use pointer arithmetic to traverse the string.
Also don't you want to print your message after the for loop?
 
Last edited:
Code:
#include <stdio.h>
#include <string.h>

int
main()
{
        char buffer[80];
        int vowels = 0;

        printf("Enter a line of text: ");
        fgets(buffer, sizeof(buffer), stdin);

        int i;
        for(i = 0; buffer[i] != '\0'; i++){
                switch(buffer[i]){
                        case 'a': case 'e': case 'i': case 'o': case 'u':
                        case 'A': case 'E': case 'I': case 'O': case 'U':
                                vowels++;
                }
        }

        printf("In text, we have %d vowels\n", vowels);
        return 0;
}

Your program can not find capitals.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 26 ·
Replies
26
Views
4K
Replies
16
Views
6K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 11 ·
Replies
11
Views
35K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
2
Views
3K
Replies
7
Views
2K
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K