C Online Compiler
Example: Remove Vowels from String in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Remove Vowels from String #include <stdio.h> #include <string.h> // For strlen #include <ctype.h> // For tolower // Function to check if a character is a vowel int isVowel(char c) { c = tolower(c); // Convert to lowercase for easier comparison return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'); } // Function to remove vowels from a string (in-place) void removeVowels(char* str) { // Step 1: Initialize two pointers: 'readIndex' to traverse the original string // and 'writeIndex' to place non-vowel characters in the modified string. int readIndex = 0; int writeIndex = 0; // Step 2: Loop through the string until the null terminator is encountered. while (str[readIndex] != '\0') { // Step 3: Check if the character at 'readIndex' is a vowel. if (!isVowel(str[readIndex])) { // Step 4: If it's NOT a vowel, copy it to the 'writeIndex' position. str[writeIndex] = str[readIndex]; // Step 5: Increment 'writeIndex' to prepare for the next non-vowel. writeIndex++; } // Step 6: Always increment 'readIndex' to move to the next character in the original string. readIndex++; } // Step 7: After the loop, null-terminate the modified string at 'writeIndex'. str[writeIndex] = '\0'; } int main() { char str1[] = "Hello World"; char str2[] = "Programming Is Fun"; char str3[] = "AEIOUaeiou"; char str4[] = "Rhythm"; printf("Original string: \"%s\"\n", str1); removeVowels(str1); printf("After removing vowels: \"%s\"\n\n", str1); printf("Original string: \"%s\"\n", str2); removeVowels(str2); printf("After removing vowels: \"%s\"\n\n", str2); printf("Original string: \"%s\"\n", str3); removeVowels(str3); printf("After removing vowels: \"%s\"\n\n", str3); printf("Original string: \"%s\"\n", str4); removeVowels(str4); printf("After removing vowels: \"%s\"\n\n", str4); return 0; }
Output
Clear
ADVERTISEMENTS