C program to eliminate all vowels from a string

In this C program, we are going to learn how to eliminate all vowels from a given string? Here, we have a string that may contain consonants, vowels etc we have to remove only vowels from it.
Submitted by Anshuman Singh, on July 05, 2019

Given a string and we have to eliminate/ remove all vowels from the string using C program.

To eliminate/remove the vowels

  • We will traverse (reach) each elements by using a loop
  • And, check the each element, if any element found as vowel, we will remove that shifting all other elements to the left
  • Finally, we will print the string - that will be a string without the vowels

Example:

    Input:
    String is: "Hello World"
    
    Output:
    String after removing vowels: "Hll Wrld"

Program to eliminate all vowels from the string in C

/* C program to eliminate all the vowels
* from the entered string
*/
#include <stdio.h>
#include <string.h>

int main()
{
    char string[50] = { 0 };
    int length = 0, i = 0, j = 0, k = 0, count = 0;

    printf("\nEnter the string : ");
    gets(string);

    length = strlen(string);
    count = length;
    for (j = 0; j < length;) {
        switch (string[j]) {
        case 'a':
        case 'A':
        case 'e':
        case 'E':
        case 'i':
        case 'I':
        case 'o':
        case 'O':
        case 'u':
        case 'U':
            for (k = j; k < count; k++) {
                string[k] = string[k + 1];
                //printf("\nstring : %s",string);
            }
            count--;
            break;
        default:
            j++;
        }
    }
    string[count] = '\0';
    printf("Final string is : %s", string);
    return 0;
}

Output

Enter the string : Hello World
Final string is : Hll Wrld

C String Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.