C Program to Delete Vowel Alphabets From a String

  • Write a C program to delete all vowels from a string.

In this C program, we will delete all vowel alphabets from a string. There are five vowel alphabets in english A, E, I, O and U. We have to delete all vowel characters from a string. If Input string is "techcrashcourse" then output should string should be "tchcrshcrs" after removing all occurrences of vowels.


C program to remove vowels from string

In this program, we first take a string as input from user using gets function. Here we are using another array to store the output string. This program uses a user defined function isVowel that takes a character as input and decide whether input character is vowel or not. Function isVowel converts upper case characters to lowercase and then perform vowel check.

We traverse from first character to last character of input string and check whether current character is vowel or not. If it is not a vowel then we copy this character to output string otherwise skip this character. At last we add a null character at the end of output string, now output string contains all input string characters except vowels.

#include <stdio.h>
#include <string.h>

int isVowel(char ch);
int main(){
    char inputString[100], outputString[100];
    int readIndex, writeIndex = 0;
    
    printf("Enter a string \n");
    gets(inputString);
    
    for(readIndex = 0;inputString[readIndex]!='\0';readIndex++){
        if(!isVowel(inputString[readIndex])){
            outputString[writeIndex++] = inputString[readIndex];
        }
    }
    outputString[writeIndex] = '\0';
    
    printf("Input String: %s \n", inputString);
    printf("String without Vowels: %s \n", outputString);
    
    return 0;
}

int isVowel(char ch){
    if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')){
        if(ch >= 'A' && ch <= 'Z'){
            ch = ch + ('a' - 'A');
        }
        /* Check if character(ch) is a vowel */
        if (ch == 'a' || ch == 'e' || ch == 'i' || 
            ch == 'o' || ch == 'u'){
            return 1;
        }     
    } 
    return 0;
}
Output
Enter a string
delete vowels
Input String: delete vowels
String without Vowels: dlt vwls

C program to delete vowels from string without using extra memory

In this program, we don't use any extra character array to store the output string without vowels. We will modify input string and delete all vowels in single pass.

#include <stdio.h>

int isVowel(char ch);
int main(){
    char inputString[100];
    int readIndex, writeIndex = 0;
    
    printf("Enter a string \n");
    gets(inputString);
    
    for(readIndex = 0;inputString[readIndex]!='\0';readIndex++){
        if(!isVowel(inputString[readIndex])){
            inputString[writeIndex++] = inputString[readIndex];
        }
    }
    inputString[writeIndex] = '\0';
    printf("String without Vowels: %s \n", inputString);
    
    return 0;
}

int isVowel(char ch){
    if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')){
        if(ch >= 'A' && ch <= 'Z'){
            ch = ch + ('a' - 'A');
        }
        /* Check if character(ch) is a vowel */
        if (ch == 'a' || ch == 'e' || ch == 'i' ||
            ch == 'o' || ch == 'u'){
            return 1;
        }     
    }
    
    return 0;
}
Output
Enter a string
without extra memory
String without Vowels: wtht xtr mmry

Related Topics
C program to remove extra spaces from string
C program to convert uppercase string to lowercase
C program to check if two strings are anagram
C program to concatenate strings
C program to convert string to integer
C program to check string is palindrome
C Program to find frequency of characters in a string
List of all C programs