C Program to Delete Vowel Alphabets From a String

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

There are five vowels 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 or delete vowels from string using extra memory

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.


/*
* C Program to remove vowels from string
*/
#include <stdio.h>
#include <conio.h>
#include <string.h>

int isVowel(char ch);
int main(){
    char inputString[100], outputString[100];
    int readIndex, writeIndex;
    printf("Enter a string \n");
    gets(inputString);
    for(readIndex = 0, writeIndex = 0; inputString[readIndex] != '\0'; readIndex++){
        if(!isVowel(inputString[readIndex])){
            /* If current character is not a vowel, copy it to outputString */
            outputString[writeIndex++] = inputString[readIndex];
        }
    }
    outputString[writeIndex] = '\0';
    
    printf("Input String: %s \n", inputString);
    printf("String without Vowels: %s \n", outputString);
    
    getch();
    return 0;
}

/*
 * Function to check whether a character is Vowel or not
 * Returns 1 if character is vowel Otherwise Returns 0 
 */
int isVowel(char ch){
    /* Check if character is lower case or upper case alphabet
    *  For any non-alphabet character return 0
    */
    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;
}
Program Output
Enter a string
delete vowels
Input String: delete vowels
String without Vowels: dlt vwls

C program to delete or remove 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.


/*
* C Program to remove vowels from string without using extra memory
*/
#include <stdio.h>
#include <conio.h>

int isVowel(char ch);
int main(){
    char inputString[100];
    int readIndex, writeIndex;
    printf("Enter a string \n");
    gets(inputString);
    for(readIndex = 0, writeIndex = 0; inputString[readIndex] != '\0'; readIndex++){
        if(!isVowel(inputString[readIndex])){
            /* If current character is not a vowel, copy it to outputString */
            inputString[writeIndex++] = inputString[readIndex];
        }
    }
    inputString[writeIndex] = '\0';
    printf("String without Vowels: %s \n", inputString);
    
    getch();
    return 0;
}

/*
 * Function to check whether a character is Vowel or not
 * Returns 1 if character is vowel Otherwise Returns 0 
 */
int isVowel(char ch){
    /* Check if character is lower case or upper case alphabet
    *  For any non-alphabet character return 0
    */
    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;
}
Program 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