delete vowels from a string in c code example
Example 1: remove vowels in string
#include <stdio.h>
int check_vowel(char);
int main()
{
char s[100], t[100];
int c, d = 0;
gets(s);
for(c = 0; s[c] != ‘\0’; c++)
{
if(check_vowel(s[c]) == 0)
{
t[d] = s[c];
d++;
}
}
t[d] = ‘\0’;
strcpy(s, t);
printf(“%s\n”, s);
return 0;
}
int check_vowel(char ch)
{
if (ch == ‘a’ || ch == ‘A’ || ch == ‘e’ || ch == ‘E’ || ch == ‘i’ || ch == ‘I’ || ch ==’o’ || ch==’O’ || ch == ‘u’ || ch == ‘U’)
return 1;
else
return 0;
}
Example 2: Remove all the vowels from a given string using pointers concept
#include<stdio.h>#include<stdlib.h>#include<string.h>#define TRUE 1#define FALSE 0 int check_vowel(char); main(){ char string[100], *temp, *pointer, ch, *start; printf("Enter a string\n"); gets(string); temp = string; pointer = (char*)malloc(100); if( pointer == NULL ) { printf("Unable to allocate memory.\n"); exit(EXIT_FAILURE); } start = pointer; while(*temp) { ch = *temp; if ( !check_vowel(ch) ) { *pointer = ch; pointer++; } temp++; } *pointer = '\0'; pointer = start; strcpy(string, pointer); free(pointer); printf("String after removing vowel is \"%s\"\n", string); return 0;} int check_vowel(char a){ if ( a >= 'A' && a <= 'Z' ) a = a + 'a' - 'A'; if ( a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u') return TRUE; return FALSE;}