Write a c program to remove or delete vowels from a string, if the input string is "c programming" then output will be "c prgrmmng". In the program we create a new string and process entered string character by character, and if a vowel is found it is not added to new string otherwise the character is added to new string, after the string ends we copy the new string into original string. Finally, we obtain a string without any vowels.
Answers
Answer:
Remove vowels from a string in C
C program to remove or delete vowels from a string: if the input string is "C programming," then the output will be "C prgrmmng." In the program, we create a new string and process input string character by character, and if a vowel is found it's excluded in the new string, otherwise the character is added to the new string after the string ends we copy the new string into the original string. Finally, we obtain a string without vowels.
C program to remove vowels from a string
#include <stdio.h>
#include <string.h>
int check_vowel(char);
int main()
{
char s[100], t[100];
int c, d = 0;
printf("Enter a string to delete vowels\n");
gets(s);
for (c = 0; s[c] != '\0'; c++) {
if (check_vowel(s[c]) == 0) { // If not a vowel
t[d] = s[c];
d++;
}
}
t[d] = '\0';
strcpy(s, t); // We are changing initial string. This is optional.
printf("String after deleting vowels: %s\n", s);
return 0;
}
int check_vowel(char t)
{
if (t == 'a' || t == 'A' || t == 'e' || t == 'E' || t == 'i' || t == 'I' || t =='o' || t=='O' || t == 'u' || t == 'U')
return 1;
return 0;
}
Output of program:
C program to delete vowels from a string output
C programming code using pointers
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int check_vowel(char);
main()
{
char string[100], *temp, *p, ch, *start;
printf("Enter a string\n");
gets(string);
temp = string;
p = (char*)malloc(100);
if( p == NULL )
{
printf("Unable to allocate memory.\n");
exit(EXIT_FAILURE);
}
start = p;
while(*temp)
{
ch = *temp;
if ( !check_vowel(ch) )
{
*p = ch;
p++;
}
temp++;
}
*p = '\0';
p = start;
strcpy(string, pointer); /* If you wish to convert original string */
free(p);
printf("String after removing vowel(s): \"%s\"\n", string);
return 0;
}
int check_vowel(char a)
{
if (a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u')
return 1;
else if (a == 'A' || a == 'E' || a == 'I' || a == 'O' || a == 'U')
return 1;
else
return 0;