Samuel
Samuel

Reputation: 640

every vowel appearing three times regardless of the input

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
    char a[100];
    int c, e=0;
    char d;
    printf("enter a text of your choice\n");
    scanf("%s",a);
    printf("enter the vowel you want to know its occurence\n");
    fflush(stdin);
    scanf("%c",&d);
    for(c=0;c<strlen(a);c++)
    {
        if(a[c]=='a'||a[c]=='o'||a[c]=='e'||a[c]=='u'||a[c]=='i')
        e++;
    }
    printf("in the text ");
    for(c=0;c<strlen(a);c++)
        printf("%c",a[c]);
    printf("\nthe vowel%c",d);
    printf(" appears %d",e);
    printf(" times.\n");
    getch();
} 

I am compiling and getting the wrong output that each and every vowel appears three times no matter the user input help.

Upvotes: 0

Views: 69

Answers (3)

Rahul
Rahul

Reputation: 3509

If you want to match the entered vowel, Change

  if(a[c]=='a'||a[c]=='o'||a[c]=='e'||a[c]=='u'||a[c]=='i')

to

 if(a[c]==d)

Upvotes: 3

wds
wds

Reputation: 32293

You read in the vowel in the variable d but then never make use of it, so you will get the counts of all vowels in your input text, provided there are no other bugs in the code.

Upvotes: 3

Henno Brandsma
Henno Brandsma

Reputation: 2166

You do not compare the characters in your text "a" to the entered character 'd' at all! So just count the number of vowels in the loop, not the occurrences of a specific character...

Upvotes: 2

Related Questions