Why Is My C Program Not Counting Vowels Correctly?

In summary, the program is testing to see if a character in the buffer is a capital, but it can not because it can only have one value. The counter is not used correctly to keep track of the number of vowels.
  • #1
chmate
37
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
  • #2
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:
  • #3
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.
 

FAQ: Why Is My C Program Not Counting Vowels Correctly?

How do I count vowels in a C program?

Counting vowels in a C program can be done by using a loop to iterate through each character in the input string. Within the loop, check if the current character is a vowel (a, e, i, o, u) and increment a counter variable if it is. Finally, print out the total count of vowels.

Can I count both uppercase and lowercase vowels?

Yes, you can count both uppercase and lowercase vowels by converting the input string to either uppercase or lowercase before starting the loop. This ensures that all vowels will be counted regardless of their case.

How do I handle special characters or numbers in the input string?

To handle special characters or numbers in the input string, you can use conditional statements within the loop to check if the current character is a vowel. If it is not a vowel, you can simply skip over it and continue to the next character in the string.

What if I want to count only a specific vowel?

If you want to count only a specific vowel, you can modify the conditional statement in the loop to check for that specific vowel. For example, if you only want to count the letter "a", the conditional statement would be: if (currentChar == 'a').

Is there a more efficient way to count vowels in a C program?

Yes, there are other ways to count vowels in a C program that may be more efficient, such as using regular expressions or built-in functions like strchr(). However, using a simple loop is a straightforward and easily understandable method for beginners.

Back
Top