im trying to write a c program that asks a user for a string and send it to a function
that counts the total number each of vowel and total number of all vowels but i am getting 0's for all can someone tell me why?

#include <stdio.h>

 int countvowels(char[]);
 int displayleachvowel(char[]);
int main()

{

   char sentence[150];

    int x, vowels = 0,eachvowel= 0;

    printf("Enter a sentence \n");

    gets(sentence);
    vowels = countvowels(sentence);
    eachvowel = displayleachvowel(sentence);
     printf("No. of vowels in %s = %d\n", sentence, vowels);
    printf("Total A's in %s =%d",sentence,eachvowel);
    printf("Total E's in %s =%d",sentence,eachvowel);
    printf("Total I's in %s =%d",sentence,eachvowel);
    printf("Total O's in %s =%d",sentence,eachvowel);
    printf("Total U's in %s =%d",sentence,eachvowel);
    return 0;
}
int countvowels(char sentence[])
{

  int x;
  int vowels = 0;

  for (x = 0; sentence[x] != '\0'; x++)     

  {

    if ((sentence[x] == 'a' || sentence[x] == 'e' || sentence[x] ==

        'i' || sentence[x] == 'o' || sentence[x] == 'u') ||

        (sentence[x] == 'A' || sentence[x] == 'E' || sentence[x] ==

        'I' || sentence[x] == 'O' || sentence[x] == 'U'))

    vowels++;

     return vowels;
  }
}

         int displayleachvowel(char sentence[])
   { 

      int x;    
      int eachvowel= 0; 
        for (x = 0; sentence[x] != '\0'; x++) 
     {

        if (sentence[x]=='a'||sentence[x]=='A')
        eachvowel++;

            if (sentence[x]=='e'||sentence[x]=='E')
            eachvowel++;

               if (sentence[x]=='i'||sentence[x]=='I')
               eachvowel++;

                 if (sentence[x]=='o'||sentence[x]=='O')
                 eachvowel++;

                  if (sentence[x]=='u'||sentence[x]=='U')
                eachvowel++;

     }

}

Dani AI

Generated

Two quick problems explain the all-zero output. First, the total-count function returns from inside the character loop so it stops after the first character. Second, is correct: the per-vowel function never returns values and it uses a single counter for A, E, I, O, U — that’s why every vowel prints the same number. The original use of gets is also unsafe (it was removed from the C standard); fgets plus trimming the newline is a safer replacement.

Suggested fixes:

  • Move the return for the total count outside the loop so the whole string is scanned.
  • Make the per-vowel routine fill separate counters (an int counts[5] or a small struct) or return a value that contains all five counts.
  • Use fgets and strip the trailing newline.
  • Use tolower((unsigned char)c) for case-insensitive checks.

A concise, safe example implementing those fixes:

#include <stdio.h>
#include <ctype.h>
#include <string.h>

void count_each_vowel(const char *s, int counts[5]) {
    for (int i = 0; i < 5; ++i) counts[i] = 0;
    for (; *s; ++s) {
        unsigned char uc = (unsigned char)*s;
        switch (tolower(uc)) {
            case 'a': counts[0]++; break;
            case 'e': counts[1]++; break;
            case 'i': counts[2]++; break;
            case 'o': counts[3]++; break;
            case 'u': counts[4]++; break;
        }
    }
}

int count_vowels(const char *s) {
    int total = 0;
    for (; *s; ++s) {
        unsigned char uc = (unsigned char)*s;
        char c = tolower(uc);
        if (c=='a' || c=='e' || c=='i' || c=='o' || c=='u') total++;
    }
    return total;
}

/* main uses fgets, removes newline, then prints totals and per-vowel counts */

This approach fixes the logic errors noted by and ensures each vowel is counted separately. For production code, consider handling arbitrarily long input, using size_t, and accounting for locale-specific characters if needed.

Your function displayleachvowel has no return statement.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.