Introduction
In this article, we will explore the topic of C Program to Remove Vowels from Even Position in a String.
String manipulation is a common task in programming, and by understanding this program, you will gain valuable insights into working with strings and manipulating them to meet specific requirements.
Also Read: C Program to Find the Inverse of 2×2 Matrix
The C programming language provides powerful features for string handling, making it an ideal choice for such tasks.
Why Remove Vowels from Even Positions?
Before we delve into the details of the program, let’s understand the motivation behind removing vowels from even positions in a string.
There may be scenarios where you need to filter out certain characters from a string based on their position or type.
Removing vowels from even positions can be useful in various applications, such as data processing, text analysis, or transforming strings for specific purposes.
Also Read: C Program to Copy the Contents of One File into Another File
C Program to Remove Vowels from Even Position in a String
Let’s now take a look at the implementation of the C program to remove vowels from even positions in a string. The program will prompt the user to enter a string and then process it to remove the vowels from even positions.
Here’s the program:
#include <stdio.h>
#include <string.h>
int isVowel(char c) {
c = tolower(c);
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
}
void removeVowelsFromEvenPositions(char *str) {
int len = strlen(str);
int i, j;
for (i = 0, j = 0; i < len; i++) {
if (i % 2 == 0 || !isVowel(str[i])) {
str[j++] = str[i];
}
}
str[j] = '\0';
}
int main() {
char str[100];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
str[strcspn(str, "\n")] = '\0'; // Remove trailing newline character
removeVowelsFromEvenPositions(str);
printf("Result: %s\n", str);
return 0;
}
Also Read: Getchar and Putchar Function in C with Example
Now, let’s break down the program and understand its different components.
The isVowel
Function
The isVowel
function is a utility function that checks whether a given character is a vowel.
It converts the character to lowercase and compares it against the vowels ‘a’, ‘e’, ‘i’, ‘o’, and ‘u’.
If the character matches any of the vowels, the function returns 1; otherwise, it returns 0.
The removeVowelsFromEvenPositions
Function
The removeVowelsFromEvenPositions
function takes a string as input and removes vowels from even positions in the string.
It iterates through each character of the input string and copies only those characters that are not vowels or are at odd positions.
The resulting string is stored back in the original string, overwriting the vowels at even positions. Finally, the function adds a null character at the end to terminate the string.
The main
Function
The main
function serves as the entry point of the program. It prompts the user to enter a string, reads the input using fgets
, and removes the trailing newline character.
Then, it calls the removeVowelsFromEvenPositions
function to modify the string. Finally, it prints the modified string as the output.
Also Read: Best 5 Programs on Fibonacci Series in C
Example Execution
Let’s see an example execution of the program to better understand its functionality:
Enter a string: Hello, World!
Result: Hlo, Wrld!
In this example, the program removes the vowels ‘e’ and ‘o’ from even positions in the string “Hello, World!” and produces the modified string “Hlo, Wrld!” as the output.
Also Read: 25 Tricky Questions on Pointers in C: Explained and Answered
FAQs
isVowel
function handle uppercase vowels? The isVowel
function converts the character to lowercase using the tolower
function before comparing it with the lowercase vowels. Therefore, it can handle both uppercase and lowercase vowels.
Yes, you can modify the program to remove vowels from odd positions by changing the condition in the removeVowelsFromEvenPositions
function. Instead of i % 2 == 0
, you can use i % 2 == 1
to achieve that.
Yes, the program is case-sensitive. It treats uppercase and lowercase letters as distinct characters. Therefore, if you want to remove vowels regardless of their case, you can modify the isVowel
function to consider both cases.
This program has a time complexity of O(n), where n is the length of the input string. It already performs well for most practical purposes. However, if you’re dealing with extremely large strings or need further optimization, you can consider using more efficient algorithms or data structures.
Absolutely! You are free to use this code as a reference or incorporate it into your own projects. However, make sure to credit the original source and understand the licensing terms, if any, associated with the code.
This program removes vowels only from even positions in a string. If you need to remove vowels from all positions or specific positions other than even, you would need to modify the program accordingly.
Conclusion
In this article, we explored the topic of C Program to Remove Vowels from Even Position in a String.
We walked through the implementation details, discussed the functions used, and provided example execution.
Understanding this program will enhance your string manipulation skills in the C programming language.
Feel free to customize the program to suit your specific requirements and experiment with different string manipulation tasks.
Remember, practice makes perfect! So keep coding and exploring the vast possibilities of C programming.