CHARACTER ARRAY PROGRAM 5

C Program to take input for a string and display it and also print total vowels in the string?

Problem In Hand

We have to take input for a sting and display it and also we have to count and print total number of  vowels ( A,E,I,O,U,a,e,i,o,u) in the string.

Process and Solution Of Problem

  1. Take input for a string
  2. set i=0, v=0
  3. Apply a while loop to read the string till end.
  4. while(n[i]!=’\0′)  : read till ‘\0’ is encountered. ‘\0’ (NULL character) is the terminating charcter of the array.
  5. read the character at i th position and display it.
  6. if the read character is “A” or “E” or “I” or “O” or “U” or “a” or “e” or “i” or “o” or “u” then we increment the count v by 1
  7. value of i is incremented by 1 to move to the next character
  8. steps 5, 6 and 7 are repeated till all the character of the string and read.
  9. further value of “v” is display which gives total number of vowels in the string.

Program/Source Code

 #include <stdio.h>
 int main()
 {
     char n[20];
     int i=0,v=0;
     printf("Enter any string ");
     gets(n); /* scanf("%s",n); */
     while(n[i]!='\0')
     {
         printf("%c\n",n[i]);
         if(n[i]=='A' || n[i]=='a' || n[i]=='E' || n[i]=='e' || n[i]=='I' || n[i]=='i' || n[i]=='O' || n[i]=='o' || n[i]=='U' || n[i]=='u')
             v++;
         i++;
     }
     printf("Total vowels in string = %d\n",v);
     return 0;
 }

Output:

Enter any string hello
h
e
l
l
o
Total vowels in string = 2