Program 81:
Explanation:#include<stdio.h> #include<string.h> main() { int i,vowelcount=0; char str[100]; printf("Enter a string\n"); gets(str); for(i=0;i<strlen(str);i++) { if(str[i]=='A'||str[i]=='a'||str[i]=='e'||str[i]=='E'||str[i]=='I' ||str[i]=='i'||str[i]=='O'||str[i]=='o'||str[i]=='U'||str[i]=='u') { vowelcount++; } } printf("%d\n",vowelcount); }
- This program starts with initializing :
- str[100] → To store string with length of 100 which means it can store 100 letters
- i →Used as helping variable
- vowelcount→ To store number of vowelsof given string and is initilized to zero.
printf("Enter a string\n"); gets(str);
takes the string from userfor(i=0;i<strlen(str);i++) { if(str[i]=='A'||str[i]=='a'||str[i]=='e'||str[i]=='E'||str[i]=='I' ||str[i]=='i'||str[i]=='O'||str[i]=='o'||str[i]=='U'||str[i]=='u') { vowelcount++; } }
It will iterate from 0 to the length of the string (i.e. from our example of "Now You See Me" it will iterate from 0 to 13 as length of string is 14.)- Each time it iterates,each character in given string is compared with either of the letters from a,e,i,o,u,A,E,I,O,U.If any character is matched with one of these letters vowelcount is incremented by 1.
- From our example "Now You See Me" has 6 vowels.
Output: