C program to delete vowels from a string : 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.

[ad type=”banner”]

C programming code

[pastacode lang=”c” manual=”%23include%20%3Cstdio.h%3E%0A%23include%20%3Cstring.h%3E%0A%20%0Aint%20check_vowel(char)%3B%0A%20%0Aint%20main()%0A%7B%0A%20%20char%20s%5B100%5D%2C%20t%5B100%5D%3B%0A%20%20int%20i%2C%20j%20%3D%200%3B%0A%20%0A%20%20printf(%22Enter%20a%20string%20to%20delete%20vowels%5Cn%22)%3B%0A%20%20gets(s)%3B%0A%20%0A%20%20for(i%20%3D%200%3B%20s%5Bi%5D%20!%3D%20’%5C0’%3B%20i%2B%2B)%20%7B%0A%20%20%20%20if(check_vowel(s%5Bi%5D)%20%3D%3D%200)%20%7B%20%20%20%20%20%20%20%2F%2Fnot%20a%20vowel%0A%20%20%20%20%20%20t%5Bj%5D%20%3D%20s%5Bi%5D%3B%0A%20%20%20%20%20%20j%2B%2B%3B%0A%20%20%20%20%7D%0A%20%20%7D%0A%20%0A%20%20t%5Bj%5D%20%3D%20’%5C0’%3B%0A%20%0A%20%20strcpy(s%2C%20t)%3B%20%20%20%20%2F%2FWe%20are%20changing%20initial%20string%0A%20%0A%20%20printf(%22String%20after%20deleting%20vowels%3A%20%25s%5Cn%22%2C%20s)%3B%0A%20%0A%20%20return%200%3B%0A%7D%0A%20%0A%20%0Aint%20check_vowel(char%20c)%0A%7B%0A%20%20switch(c)%20%7B%0A%20%20%20%20case%20’a’%3A%0A%20%20%20%20case%20’A’%3A%0A%20%20%20%20case%20’e’%3A%0A%20%20%20%20case%20’E’%3A%0A%20%20%20%20case%20’i’%3A%0A%20%20%20%20case%20’I’%3A%0A%20%20%20%20case%20’o’%3A%0A%20%20%20%20case%20’O’%3A%0A%20%20%20%20case%20’u’%3A%0A%20%20%20%20case%20’U’%3A%0A%20%20%20%20%20%20return%201%3B%0A%20%20%20%20default%3A%0A%20%20%20%20%20%20return%200%3B%0A%20%20%7D%0A%7D” message=”” highlight=”” provider=”manual”/]

Output of program:

C program to delete vowels from a string

C programming code using pointers

[ad type=”banner”] [pastacode lang=”c” manual=”%23include%3Cstdio.h%3E%0A%23include%3Cstdlib.h%3E%0A%23include%3Cstring.h%3E%0A%23define%20TRUE%201%0A%23define%20FALSE%200%0A%20%0Aint%20check_vowel(char)%3B%0A%20%0Amain()%0A%7B%0A%20%20%20char%20string%5B100%5D%2C%20*temp%2C%20*pointer%2C%20ch%2C%20*start%3B%0A%20%0A%20%20%20printf(%22Enter%20a%20string%5Cn%22)%3B%0A%20%20%20gets(string)%3B%0A%20%0A%20%20%20temp%20%3D%20string%3B%0A%20%20%20pointer%20%3D%20(char*)malloc(100)%3B%0A%20%0A%20%20if(%20pointer%20%3D%3D%20NULL%20)%0A%20%20%20%7B%0A%20%20%20%20%20%20printf(%22Unable%20to%20allocate%20memory.%5Cn%22)%3B%0A%20%20%20%20%20%20exit(EXIT_FAILURE)%3B%0A%20%20%20%7D%0A%20%0A%20%20%20start%20%3D%20pointer%3B%0A%20%0A%20%20%20while(*temp)%0A%20%20%20%7B%0A%20%20%20%20%20%20ch%20%3D%20*temp%3B%0A%20%0A%20%20%20%20%20%20if%20(%20!check_vowel(ch)%20)%0A%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20*pointer%20%3D%20ch%3B%0A%20%20%20%20%20%20%20%20%20pointer%2B%2B%3B%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20temp%2B%2B%3B%0A%20%20%20%7D%0A%20%20%20*pointer%20%3D%20’%5C0’%3B%0A%20%0A%20%20%20pointer%20%3D%20start%3B%0A%20%20%20strcpy(string%2C%20pointer)%3B%20%2F*%20If%20you%20wish%20to%20convert%20original%20string%20*%2F%0A%20%20%20free(pointer)%3B%0A%20%0A%20%20%20printf(%22String%20after%20removing%20vowel%20is%20%5C%22%25s%5C%22%5Cn%22%2C%20string)%3B%0A%20%0A%20%20%20return%200%3B%0A%7D%0A%20%0Aint%20check_vowel(char%20a)%0A%7B%0A%20%20%20if%20(%20a%20%3E%3D%20’A’%20%26%26%20a%20%3C%3D%20’Z’%20)%0A%20%20%20%20%20%20a%20%3D%20a%20%2B%20’a’%20-%20’A’%3B%0A%20%0A%20%20%20if%20(%20a%20%3D%3D%20’a’%20%7C%7C%20a%20%3D%3D%20’e’%20%7C%7C%20a%20%3D%3D%20’i’%20%7C%7C%20a%20%3D%3D%20’o’%20%7C%7C%20a%20%3D%3D%20’u’)%0A%20%20%20%20%20%20return%20TRUE%3B%0A%20%0A%20%20%20return%20FALSE%3B%0A%7D” message=”” highlight=”” provider=”manual”/]

Categorized in: