how to devowel string in c program
To devowel a string in a C program, you can follow these steps:
Declare and initialize the string: Start by declaring and initializing a character array to hold the input string. For example, you can use the
char
data type and assign a string literal to it.Create a function to devowel the string: Define a function that takes the input string as a parameter and removes the vowels from it. This function can be called
devowelString
or any other name you prefer.Iterate over the string: Use a loop, such as a
for
loop, to iterate over each character in the input string.Check if the character is a vowel: Inside the loop, use a conditional statement, such as an
if
statement, to check if the current character is a vowel. Vowels include 'a', 'e', 'i', 'o', and 'u' (both lowercase and uppercase).Remove the vowel: If the current character is a vowel, you can simply skip it and move on to the next character. If the character is not a vowel, you can append it to a new string or overwrite the original string with the non-vowel characters.
Terminate the string: After removing the vowels, make sure to add a null character (
\0
) at the end of the resulting string to indicate its end.Print or use the devoweled string: Finally, you can print the devoweled string or use it in further computations, depending on your specific requirements.
Here's an example implementation of the steps described above:
#include <stdio.h>
void devowelString(char* str) {
int i, j;
for (i = 0, j = 0; str[i] != '\0'; i++) {
if (str[i] != 'a' && str[i] != 'e' && str[i] != 'i' && str[i] != 'o' && str[i] != 'u' &&
str[i] != 'A' && str[i] != 'E' && str[i] != 'I' && str[i] != 'O' && str[i] != 'U') {
str[j++] = str[i];
}
}
str[j] = '\0';
}
int main() {
char str[] = "Hello, World!";
devowelString(str);
printf("Devoweled string: %s\n", str);
return 0;
}
This program takes the input string "Hello, World!" and removes all the vowels from it. The resulting devoweled string is then printed as output: "Hll, Wrld!".