C Examples

WAP in C to input a text and print the word containing the maximum number of vowels

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.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

C# Program to Delete Element at Desired position From Array

The Program in C# Program to Delete Element at Desired position From Array is given below: Output: Hello Codeauri Family, enter the number of elements in the array:4Enter…

Leave a Reply

Your email address will not be published. Required fields are marked *

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X