Skip to main content

ProwessApps

Learn · Practice · Excel

C Program to remove all vowels from a string

C Code Example — String Programs

ADVERTISEMENT

C Program to remove all vowels from a string

Objective

Write a C program to remove all vowels from a string.

Algorithm / Approach

  1. Create a helper function check(char c) containing a switch statement that returns 1 if the character is a vowel, and 0 otherwise.
  2. In main(), loop through the original string.
  3. If check(a[i]) == 0 (meaning it is a consonant or symbol), copy it to a new array b[j] and increment j.
  4. Append the null terminator to b and print it.
main.c
#include<stdio.h>
int check(char); 
int main( ) {
 char a[100], b[100];
 int i, j = 0;
 printf("Enter a string : ");
 gets(a);
 for(i = 0; a[i] != '\0'; i++) {
  if(check(a[i]) == 0) {       
   b[j] = a[i];
   j++;
  }
 }
 b[j] = '\0';
 printf("String after removing vowels : %s\n", b);
 return 0;
}

int check(char c) {
  switch(c) {
   case 'a':
   case 'A':
   case 'e':
   case 'E':
   case 'i':
   case 'I':
   case 'o':
   case 'O':
   case 'u':
   case 'U':
      return 1;
   default:
      return 0;
  }
}

Expected Output

Enter a string : prowess
String after removing vowels : prwss

Explanation of the Program

  • This program uses a filtering pattern. We walk through the source string, but we only copy characters over to the destination string if they pass our vowel test.
  • Notice how the switch statement is structured: multiple cases are stacked together without break statements. This allows all vowels to "fall through" and execute the exact same return 1 instruction.

Complexity

Time Complexity O(n)
Space Complexity O(n) - Uses a second array to store the filtered result.
ADVERTISEMENT