C Online Compiler
Example: Remove Vowels from String using Switch Case in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Remove Vowels from String using Switch Case #include <stdio.h> // Required for printf, gets (use fgets for safety) #include <string.h> // Required for strlen #include <ctype.h> // Required for tolower #define MAX_LENGTH 100 int main() { char str[MAX_LENGTH]; char modifiedStr[MAX_LENGTH]; // Optional: to store in a new string int i, j = 0; // Step 1: Prompt user for input string printf("Enter a string: "); // Using fgets is safer than gets to prevent buffer overflows // Read up to MAX_LENGTH-1 characters, store in str, from stdin fgets(str, MAX_LENGTH, stdin); // Remove the trailing newline character that fgets might read str[strcspn(str, "\n")] = 0; // Step 2: Iterate through the original string for (i = 0; str[i] != '\0'; i++) { // Step 3: Convert current character to lowercase for case-insensitive check char currentChar = tolower(str[i]); // Step 4: Use switch case to check if the character is a vowel switch (currentChar) { case 'a': case 'e': case 'i': case 'o': case 'u': // It's a vowel, do not copy it. Skip to the next character. break; default: // It's a consonant or non-alphabetic character, copy it. modifiedStr[j] = str[i]; j++; // Increment the write index break; } } // Step 5: Null-terminate the modified string modifiedStr[j] = '\0'; // Step 6: Print the modified string printf("String after removing vowels: %s\n", modifiedStr); return 0; }
Output
Clear
ADVERTISEMENTS