This blog is under construction

Sunday 23 June 2013

C program to find the number of vowels in a given string

Write a C program to find the number of vowels in a given string.


  #include <stdio.h>
  int main() {
        char str[100];
        char vowel[] = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'};
        int n = 0, i = 0, j;

        /* get the input from user */
        printf("Enter your input:");
        fgets(str, 100, stdin);

        /* find the number of vowel in give string */
        while (str[i] != '\n') {
                for (j = 0; j < 10; j++) {
                        if (str[i] == vowel[j]) {
                                n++;
                                break;
                        }
                }
                i++;
        }

        /* print the output */
        printf("No of vowels present in given input: %d\n", n);
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter your input:Apple and orange
  No of vowels present in given input: 6



No comments:

Post a Comment