Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Convert vowels from upper to lower or lower to upper using C program
In C programming, we can convert vowels in a string from uppercase to lowercase or lowercase to uppercase using character manipulation functions. This involves checking each character to identify vowels and then applying the appropriate case conversion.
Syntax
#include <ctype.h> int toupper(int c); // Converts to uppercase int tolower(int c); // Converts to lowercase
Method 1: Convert Lowercase Vowels to Uppercase
This approach converts all lowercase vowels (a, e, i, o, u) to their uppercase equivalents while keeping other characters unchanged −
#include <stdio.h>
#include <ctype.h>
int main() {
char string[100];
int i;
printf("Enter the string: ");
fgets(string, sizeof(string), stdin);
for(i = 0; string[i] != '\0'; i++) {
if(string[i] == 'a' || string[i] == 'e' || string[i] == 'i' ||
string[i] == 'o' || string[i] == 'u') {
string[i] = toupper(string[i]);
}
}
printf("Result: %s", string);
return 0;
}
Enter the string: hello world Result: hEllO wOrld
Method 2: Convert Uppercase Vowels to Lowercase
This approach converts all uppercase vowels (A, E, I, O, U) to their lowercase equivalents −
#include <stdio.h>
#include <ctype.h>
int main() {
char string[100];
int i;
printf("Enter the string: ");
fgets(string, sizeof(string), stdin);
for(i = 0; string[i] != '\0'; i++) {
if(string[i] == 'A' || string[i] == 'E' || string[i] == 'I' ||
string[i] == 'O' || string[i] == 'U') {
string[i] = tolower(string[i]);
}
}
printf("Result: %s", string);
return 0;
}
Enter the string: HELLO WORLD Result: HeLLo WoRLD
Method 3: Toggle Case of All Vowels
This approach toggles the case of vowels − uppercase vowels become lowercase and lowercase vowels become uppercase −
#include <stdio.h>
#include <ctype.h>
int main() {
char string[100];
int i;
printf("Enter the string: ");
fgets(string, sizeof(string), stdin);
for(i = 0; string[i] != '\0'; i++) {
if(string[i] == 'a' || string[i] == 'e' || string[i] == 'i' ||
string[i] == 'o' || string[i] == 'u') {
string[i] = toupper(string[i]);
}
else if(string[i] == 'A' || string[i] == 'E' || string[i] == 'I' ||
string[i] == 'O' || string[i] == 'U') {
string[i] = tolower(string[i]);
}
}
printf("Result: %s", string);
return 0;
}
Enter the string: Hello World Result: hEllO wOrld
Key Points
- Use
fgets()instead ofgets()for safe string input - Include
<ctype.h>header fortoupper()andtolower()functions - Check both uppercase and lowercase vowels for complete conversion
- The string is modified in-place, changing the original content
Conclusion
Vowel case conversion in C can be achieved by iterating through the string and checking each character against vowel patterns. The toupper() and tolower() functions provide safe and standard methods for case conversion.
