Program in C to delete vowels from a string in c:

Vowels in English literature are the letters a,e,i,o,u. Remember that in a computer the characters are stored as ASCII values hence a and A both are different characters for the compiler. We would hence have to scan for both the lowercase and uppercase vowels if any in the string and delete it.

APPROACH 1: Using user-defined function:

While traversing the char array, we call the function find_vowel to return a flag i.e value 0 or 1 which would let us know if the

Code:

#include <stdio.h>

#include <string.h>



int find_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;   //It is a vowel

else

return 0;   //It is not a vowel

}



int main()

{

char str[100], temp[100];

int i, j;



printf("Enter a string: \n");

fgets(str, 100, stdin);



for(i = 0, j = 0; str[i] != '\0'; i++)

{

if(find_vowel(str[i]) == 0)

{

temp[j] = str[i];                           //It is not a vowel

j++;

}

}



temp[j] = '\0';  //terminate the string



strcpy(str, temp);    //modifying the original string with vowels deleted.



printf("String after deleting vowels: %s\n", str);



return 0;

}

Output:

Enter a string:

How are you?

String after deleting vowels: Hw r y?

APPROACH 2: Using pointers

Here the approach is the same as above, however, it is used in conjunction with pointers.

Code:

#include<stdio.h>

#include<stdlib.h>

#include<string.h>



int find_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;   //It is a vowel

else

return 0;   //It is not a vowel

}





int main()

{

char *string, *temp, *strptr, ch, *start;

int size = 100;



printf("Enter a string\n");

string = (char*)malloc(size);

getline(&string, &size, stdin);



temp = string;

strptr = (char*)malloc(100);



start = strptr;



while(*temp)

{

ch = *temp;



if ( !find_vowel(ch) )

{

*strptr = ch;

strptr++;

}

temp++;

}

*strptr = '\0';



strptr = start;

strcpy(string, strptr);

free(strptr);



printf("String after removing vowels: %s\n", string);



return 0;

}

Output:

Enter a string

How do you do ?

String after removing vowels: Hw d y d ?

Report Error/ Suggestion

Related Posts:

[yuzo_views]











CopyRight © 2019

CopyRight © 2019