Han Sen
Han Sen

Reputation: 77

C program to count how many vowels in a list of input words

I need to write a program to count how many vowels (‘a’, ‘e’, ‘i’, ‘o’, ‘u’) in a list of input words. My program reads the words one by one and prints the number of vowels occurring in each word. Each word consists of only alphabets in mixed cases. The program repeats this process until hitting the word “exit” (case-insensitive). In this case, terminate the program after printing the number of vowels in “exit”. After termination, the program shall ignore remaining inputs, if any.

Input: Multiple words spanning across lines. Each word shall consists of no more than 50 characters. The words are separated by white spaces.

Output: The number of vowels in each of the input word, separated by newline. That is, one number on a line.

Example expected input output

Input:

I go to school by BUS

Exit

Output:

1

1

1

2

0

1

2

Another Input:

I

went

apple

school

by

BUS

Exit

Output:

1

1

2

2

0

1

2

I am having problem with "Time limit exceed" and i do appreciate any feedback to my program

when i input:

I go to school by BUS

i got a notification Time limit exceed and an output :

1

1

1

2

0

1

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

int main(void)
{
char s1[51], ex[5];
int N, i, v, newline=1;
while(newline){
fgets( s1, 51, stdin);
if((s1[0]=='e' || s1[0]=='E')&&(s1[1]=='x' || s1[1]=='X')&&(s1[2]=='i' ||       s1[2]=='I')&&(s1[3]=='t' || s1[3]=='T')&&(s1[4]=='\0' || s1[4]==' ')){
    printf("2\n"); // to check whether the input is Exit
    newline=0;
}
else{
    N=strlen(s1);
    for(i=0;i<N;i++){
        if(s1[i]=='a' || s1[i]=='o' || s1[i]=='e' || s1[i]=='i' || s1[i]=='u' || s1[i]=='A' || s1[i]=='O' || s1[i]=='E' || s1[i]=='I' || s1[i]=='U'){
            v++; // to calculate the number of vowels
                 }
         if(s1[i+1]==' '){ // if next entry is spacebar, print the number of vowels in the current word and add i so that next loop will start on new word
            printf("%d\n", v);
            v=0;
            i++;
        }
        else if(s1[i+1]=='\n'){ // if next entry is new line, print the number of vowels in the current word, restart the whole program by exiting the for loop 
            printf("%d\n", v);
            v=0;
            newline=1;
            break;
        }

        }

        }
    }


return 0;

}

Upvotes: 2

Views: 1484

Answers (2)

Dorin
Dorin

Reputation: 1086

You should review few things:

  1. When compare char, case-insensitive, is easy to do like that:

    char c1, c2; if ( toupper(c1) == toupper(c1) ) { do_something(); }

  2. To exit from a loop, I would use break; instead of return;

The whole code would be this:

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

int isVowel(char c);
int isExit(char* c);

int main(void)
{
    char s1[51];
    int N, i, v = 0;
    int noExit = 1;

    while( noExit ){
        fgets( s1, 51, stdin);
        N=strlen(s1);

        for(i=0;i<N;i++) {
            if ( isExit(&s1[i]) ) {
                 printf("2\n");
                 noExit = 0;
                 break;
            }
            else{
                if( isVowel(s1[i]) ) {
                    v++;
                }
                else if( s1[i]==' ' || s1[i]=='\n' ) {
                    printf("%d\n", v);
                    v=0;
                }
            }
        }
    }
    return 0;
}

int isVowel(char c) {
    c = toupper(c);
    if( c=='A' || c=='E' || c=='I' || c=='O' || c=='U' )
        return 1;
    else
        return 0;
}

int isExit(char* c) {
    if ( (toupper(c[0]) == 'E') && (toupper(c[1]) == 'X') &&
         (toupper(c[2]) == 'I') && (toupper(c[3]) == 'T')    ) {
        return 1;
    }
    else {
        return 0;
    }
}

Upvotes: 1

Rishikesh Raje
Rishikesh Raje

Reputation: 8614

You have an infinite loop. newline will never be 0. and the outer while loop will never exit.

So, you need to change the newline=1 in the last else if condition to newline=0.

In addition, the variable v is not initialized, so you are not getting the first answer correctly. You should set v=0 at the beginning before the while loop starts.

Upvotes: 0

Related Questions