Remove vowels from a string in C

In this program we will remove or delete vowels from the given string. We will take a string as input from the user then check all the character one by one. If we find any vowel inside it then we will delete that vowels. After checking all the character we will print the output a new string without any vowel.

If a user gives the input “competitive programmer” then we will print after deleting all vowels from it “cmpttv prgrmmr”. We can do this with recursion or without it, as well as pointer. Let’s see some C program to remove vowel from a string.

C program to delete vowels from a string

// remove vowel program

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

int remove_vowel(char cha){    // remove vowel function
  if(cha == 'a' || cha == 'A' || cha == 'e' || cha == 'E' || cha == 'i'
     || cha == 'I' || cha =='o' || cha =='O' || cha == 'u' || cha == 'U'){

    return 1;
  }

  return 0;
}


int main(){
  char with_vowel[200], without_vowel[200];
  int x, y = 0;

  printf("Enter the string to delete all vowels from it : \n");
  gets(with_vowel);

  for(x = 0; with_vowel[x] != '\0'; x++){

    if(remove_vowel(with_vowel[x]) == 0){
      without_vowel[y] = with_vowel[x];
      y++;
   }
  }

  without_vowel[y] = '\0';

  strcpy(with_vowel, without_vowel);

  printf("\nYour given string after deleting all vowel is : \n%s\n", with_vowel);

  return 0;
}

Output of delete vowels program:

delete-vowel-from-a-string-in-c-c-program-to-remove-vowel

Delete vowels program using pointer in C

// remove vowel using pointer in c

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

int remove_vowel(char x){
  if(x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u'){
    return 1;

  }else if(x == 'A' || x == 'E' || x == 'I' || x == 'O' || x == 'U'){
    return 1;

  }else{
    return 0;
  }
}


int main(){
   char main_string[200], *strp, *p, cha, *begin;

   printf("Enter any string for removing vowels : \n");
   gets(main_string);

   strp = main_string;
   p = (char*)malloc(200);

   if(p == NULL){
      printf("Error ! not able to allocate memory.\n");
      exit(EXIT_FAILURE);
   }

   begin = p;

   while(*strp){
      cha = *strp;

      if (!remove_vowel(cha)){
         *p = cha;
         p++;
      }
      strp++;
   }

   *p = '\0';

   p = begin;
   strcpy(main_string, p);
   free(p);

   printf("\nGiven string after removing all vowel is : \n%s\n", main_string);

   return 0;
}

Output :

Enter any string for removing vowels :
competitive programmer

Given string after removing all vowel is :
cmpttv prgrmmr

Previous pageReverse string

Next pageSort string