The Program in C to input a text and print the word containing the maximum number of vowels is given below:
#include <stdio.h>
#include <string.h>
int main() {
char text[100], maxWord[100];
int i, j, k, maxVowels = 0, vowels, len;
printf("Enter a text: ");
fgets(text, sizeof(text), stdin); // using fgets instead of gets
len = strlen(text);
j = 0;
for (i = 0; i <= len; i++) {
if (text[i] == ' ' || text[i] == '\0') {
vowels = 0;
for (k = j; k < i; k++) {
if (text[k] == 'a' || text[k] == 'e' || text[k] == 'i' || text[k] == 'o' || text[k] == 'u' || text[k] == 'A' || text[k] == 'E' || text[k] == 'I' || text[k] == 'O' || text[k] == 'U') {
vowels++;
}
}
if (vowels > maxVowels) {
maxVowels = vowels;
k = 0;
for (j; j < i; j++) {
maxWord[k++] = text[j];
}
maxWord[k] = '\0';
}
j = i + 1;
}
}
printf("Word containing the maximum number of vowels: %s", maxWord);
return 0;
}
Output:
Enter a text: ENGINEER ARE SUPER COOL
Word containing the maximum number of vowels: ENGINEER
Pro-Tips💡
This version uses fgets
function to read the input text, this function reads the input stream and stores it in a string buffer
and it also accept additional parameter of maximum number of characters to be read and it also add a safety check to prevent buffer overflow.
Learn C-Sharp ↗
C-sharp covers every topic to learn about C-Sharp thoroughly.
Learn C Programming ↗
C-Programming covers every topic to learn about C-Sharp thoroughly.
Learn C++ Programming↗
C++ covers every topic to learn about C-Sharp thoroughly.