Write a C program to print the words starting with vowel.
#include <string.h>
/* checks whether the given character is vowel */
int isVowel(char ch) {
int i;
char vowels[] = {'a', 'e', 'i', 'o', 'u',
'A', 'E', 'I', 'O', 'U'};
for (i = 0; i < 10; i++) {
if (ch == vowels[i]) {
return 1;
}
}
return 0;
}
int main() {
char myStr[256], word[256];
int i = 0, j = 0, ch, count = 0;
/* get the input string from the user */
printf("Enter your input string:");
fgets(myStr, 256, stdin);
myStr[strlen(myStr) - 1] = '\0';
printf("Words Starting With Vowel:\n");
/*
* copying word by word and prints words
* starting with vowels.
*/
while (myStr[i] != 0) {
if(myStr[i] == ' ') {
word[j] = '\0';
j = 0;
/*
* checks whether the word starts
* with vowel
*/
if (isVowel(word[j])) {
printf("%s\n", word);
count++;
}
i++;
continue;
}
word[j++] = myStr[i];
i++;
}
word[j] = '\0';
/* print if the last word in the string is vowel */
if (isVowel(word[0])) {
printf("%s\n", word);
count++;
}
/* prints the number of words starting with vowel */
printf("Number of word starting with vowels: %d\n", count);
return 0;
}
Output:
jp@jp-VirtualBox:~/$ ./a.out
Enter your input string:A old man lived alone in india
Words Starting With Vowel:
A
old
alone
in
india
Number of word starting with vowels: 5
Enter your input string:A old man lived alone in india
Words Starting With Vowel:
A
old
alone
in
india
Number of word starting with vowels: 5
No comments:
Post a Comment