Vowel Expansion
Expand a string by inserting additional vowels next to each existing vowel based on specific rules (a/A: 1 extra, e/E: 2 extra, i/I: 3 extra, o/O: 4 extra, u/U: 5 extra)
Understand the Problem
Problem Statement
Vowel Expansion: The program must accept a string S as the input. The program must expand the string S by inserting vowels after each vowel in it based on the following conditions.
– If the vowel is a or A, then the program must insert one more a or A next to it.
– If the vowel is e or E, then the program must insert two more e or E next to it.
– If the vowel is i or I, then the program must insert three more i or I next to it.
– If the vowel is o or O, then the program must insert four more o or O next to it.
– If the vowel is u or U, then the program must insert five more u or U next to it.
Finally, the program must print the modified string S as the output.
Note: The case of the vowels to be inserted must be the same as the case of the vowels present in the string S.
Constraints
- 1 <= Length of S <= 100
- String S can contain alphabets, digits, and special characters
- Vowels are case-sensitive (a/A, e/E, i/I, o/O, u/U are treated separately)
- The expanded vowels maintain the same case as the original vowel
Examples
ArgumentAArguuuuuumeeentThere are 3 vowels: A (adds 1 more A), u (adds 5 more u), e (adds 2 more e). Result: A + A + rg + u + uuuuu + m + e + ee + nt = AArguuuuuumeeent
LETUSCrack#123LEEETUUUUUUSCraack#123Vowels: E (adds 2 more E), U (adds 5 more U), a (adds 1 more a), c (adds 2 more c). Result: L + E + EEE + T + U + UUUUU + SC + r + a + aa + ck#123 = LEEETUUUUUUSCraack#123
ABCDEiouAABCDEEEiiiiooooouuuuuuVowels: A (adds 1 more A), E (adds 2 more E), i (adds 3 more i), o (adds 4 more o), u (adds 5 more u). Result: A + AA + B + C + D + E + EEE + i + iiii + o + oooo + u + uuuuuu = AABCDEEEiiiiooooouuuuuu
Solution
#include <stdio.h>
#include <string.h>
#include <ctype.h>
define MAX_LEN 200
typedef struct {
char vowels[10];
int count;
} VowelExpansion;
void expandVowel(char c, VowelExpansion *result) {
result->count = 1;
result->vowels[0] = c;
if (c == 'a' || c == 'A') {
result->vowels[1] = c;
result->count = 2;
} else if (c == 'e' || c == 'E') {
result->vowels[1] = c;
result->vowels[2] = c;
result->count = 3;
} else if (c == 'i' || c == 'I') {
result->vowels[1] = c;
result->vowels[2] = c;
result->vowels[3] = c;
result->count = 4;
} else if (c == 'o' || c == 'O') {
result->vowels[1] = c;
result->vowels[2] = c;
result->vowels[3] = c;
result->vowels[4] = c;
result->count = 5;
} else if (c == 'u' || c == 'U') {
result->vowels[1] = c;
result->vowels[2] = c;
result->vowels[3] = c;
result->vowels[4] = c;
result->vowels[5] = c;
result->count = 6;
}
}
int isVowel(char c) {
return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' ||
c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');
}
int main() {
char input[MAX_LEN];
char output[MAX_LEN * 6]; // Worst case: every char is 'u' (6x expansion)
int outputIndex = 0;
VowelExpansion expansion;
fgets(input, sizeof(input), stdin);
// Remove newline if present
int len = strlen(input);
if (len > 0 && input[len-1] == '\n') {
input[len-1] = '\0';
len--;
}
for (int i = 0; i < len; i++) {
if (isVowel(input[i])) {
expandVowel(input[i], &expansion);
// Add expanded vowels
for (int j = 0; j < expansion.count; j++) {
output[outputIndex++] = expansion.vowels[j];
}
}
// Always add the original character
output[outputIndex++] = input[i];
}
output[outputIndex] = '\0';
printf("%s\n", output);
return 0;
}C Solution Explanation:
- Structure Definition:
VowelExpansionstores expanded vowels and count - expandVowel Function: Takes a character and fills the structure with the appropriate number of additional vowels based on the vowel type
- isVowel Function: Helper to check if a character is a vowel
- Main Logic:
- Read input string using
fgets() - For each character, check if it's a vowel
- If vowel: call
expandVowel()and add all expanded vowels to output - Always add the original character to maintain string structure
- Read input string using
- Output: Print the final expanded string
The solution handles case sensitivity correctly and uses character arrays for string manipulation.