1

I just want to print highest numbered vowel.
For ex: the cat is the cat. In this sentence,

the vowel a is repeated 2 times  
the vowel e is repeated 2 times  
the vowel i is repeated 1 times  
the vowel o is repeated 0 times  
the vowel u is repeated 0 times  

I just want to see a and e in the console.

Here is the code.

void vowel(char st2[]){

   char vow[]="aeiou"; //vowel
   int i=0,j=0,count=0;
   while(vow[i]!='\0'){ //loop until sentence ends
       count=0;
       for(j=0;j<strlen(st2);j++) 
       {
           if(st2[j]==vow[i]){ //increment the counter by one if one letter in the sentence is equal to the vowels
               count++;                   
           }
       }
      
       printf("the vowel %c is repeated %d times\n",vow[i],count);
       i++;
   }
}
5
  • What is the current behaviour of the code? If something's wrong, what is it? Commented Aug 10, 2022 at 13:35
  • not wrong, code prints all vowels, just I want to change this Commented Aug 10, 2022 at 13:38
  • @MMZK1526 as clarified by an edit. Commented Aug 10, 2022 at 13:40
  • Why not keep an array of counts, one for each vowel, and do not print anything in the while loop. After having the count of each vowel in the counts array, print out only the index corresponding to the highest number. Commented Aug 10, 2022 at 13:41
  • Don't put strlen in your loop test. It needlessly slows your code. Since you're using C, performance must be an issue, so don't write code like this. Think about it. If you were doing it by hand, would you count the length of the string each and every time? Of course not. So why is your code doing it? It makes no sense. Commented Aug 10, 2022 at 14:01

2 Answers 2

1

Here, in this version, we count how many occurrences there are of a vowel, and only print the highest one.

void vowel(char st2[]){

   char vow[]="aeiou"; //vowel
   int length = strlen(st2);
   int counts[] = {0,0,0,0,0};
   int i=0,j=0,count=0;
   while(vow[i]!='\0'){ //loop until sentence ends
       count=0;
       for(j=0;j<length;j++) 
       {
           if(st2[j]==vow[i]){ //increment the counter by one if one letter in the sentence is equal to the vowels
               count++; 
               counts[i] = count;                  
           }
       }
       i++;
   }
   int highest = 0;
   for(int i = 0; i < 5; i++){
    if(counts[i] > highest){
        highest = counts[i];
    }
   }
   for(int i = 0; i< 5; i++){
    if(counts[i] == highest){
        printf("the vowel %c is repeated %d times\n",vow[i],highest);
    }
   }
}
Sign up to request clarification or add additional context in comments.

Comments

0

The elegance of C is that one can obtain functionality with very little code.

int main() {
    char *str = "The quick brown fox jumps over the lazy dog";
    char which = 'a'; // default
    int maxCnt = 0;

    for( char *pv = "aeiou"; *pv; pv++ ) {
        int cnt = 0;
        for( char *cp = str; *cp; cp++ )
            cnt += ( *cp == *pv );

        if( cnt > maxCnt ) {
            maxCnt = cnt;
            which = *pv;
        }
    }
    if( maxCnt )
        printf( "Vowel '%c' occurs %d times.\n", which, maxCnt );
    else
        printf( "NO vowels found in '%s'.\n", str );

    return 0;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.