Why Is My C Program Not Counting Vowels Correctly?

  • Thread starter Thread starter chmate
  • Start date Start date
  • Tags Tags
    Counting Program
AI Thread 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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top