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
SUMMARY

The forum discussion addresses a C program intended to count vowels in a given text but fails due to logical errors in the vowel-checking condition. The original code incorrectly uses a logical AND operator to check for multiple vowels simultaneously, which is impossible. The corrected version utilizes a switch statement to count both lowercase and uppercase vowels, storing the count in a dedicated variable. The final output correctly displays the total number of vowels after processing the input string.

PREREQUISITES
  • Understanding of C programming syntax and structure
  • Familiarity with control flow statements (if, switch)
  • Knowledge of string handling in C, including the use of fgets
  • Basic understanding of variable types and scope in C
NEXT STEPS
  • Explore C programming string manipulation techniques
  • Learn about pointer arithmetic in C for efficient string traversal
  • Study the use of switch statements for conditional logic in C
  • Investigate methods for handling case sensitivity in string comparisons
USEFUL FOR

C programmers, software developers, and students learning about string manipulation and control flow in C programming.

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.
 

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