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
Alternate vowel and consonant string in C/C++?
We are given a string with both vowels and consonants. Our task is to rearrange it so that vowels and consonants appear alternately, while keeping their original order within their groups.
This rearrangement is only possible if the number of vowels and consonants is equal, or their difference is exactly one. If multiple valid arrangements are possible, we return the one that is lexicographically smaller.
Syntax
char* alternateVowelConsonant(char* str); bool isVowel(char ch);
Example Scenarios
Scenario 1
<b>Input:</b> "objective" <b>Output:</b> "obejctive" <b>Explanation:</b> Vowels = [o, e, i, e], Consonants = [b, j, c, t, v] Consonants are more by 1 ? valid for alternating arrangement We start with a vowel ? o ? b ? e ? j ? i ? c ? e ? t ? v
Scenario 2
<b>Input:</b> "aepr" <b>Output:</b> "aper" <b>Explanation:</b> Vowels = [a, e], Consonants = [p, r] Both groups have equal count ? valid for alternating arrangement Two valid combinations: a ? p ? e ? r ? "aper" p ? a ? r ? e ? "pare" We compare both and choose the lexicographically smaller one ? "aper".
Algorithm
The following are the steps −
- First, we count the number of vowels and consonants in the input string by checking each character.
- If the absolute difference between the count of vowels and consonants is more than 1, it is not possible to arrange them in alternating order. In that case, we return an empty string.
- Then, we create two separate strings, one for vowels and the other for consonants, while preserving their original order.
- If both vowels and consonants are equal in number, we can start with either. We choose the lexicographically smaller starting character.
- If the number of vowels is greater, we begin the result string with a vowel and then alternate characters from both strings.
- If the number of consonants is greater, we start the result string with a consonant and follow the same process to build the final result.
Example: C Program to Alternate Vowels and Consonants
Here is a C program that rearranges the letters of a string so that vowels and consonants appear alternately −
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ctype.h>
#define MAX_LEN 100
bool isVowel(char ch) {
ch = tolower(ch);
return (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u');
}
char* alternateVowelConsonant(const char* s) {
static char result[MAX_LEN];
char vowels[MAX_LEN] = "", consonants[MAX_LEN] = "";
int vi = 0, ci = 0;
/* Separate vowels and consonants */
for (int i = 0; s[i]; i++) {
if (isVowel(s[i]))
vowels[vi++] = s[i];
else
consonants[ci++] = s[i];
}
vowels[vi] = '\0';
consonants[ci] = '\0';
/* Check if alternating is possible */
if (abs(vi - ci) >= 2) {
strcpy(result, "Not possible");
return result;
}
int i = 0, j = 0, k = 0;
bool startWithVowel = vi >= ci;
/* Build alternating string */
while (i < vi || j < ci) {
if (startWithVowel && i < vi)
result[k++] = vowels[i++];
if (j < ci)
result[k++] = consonants[j++];
startWithVowel = true;
}
result[k] = '\0';
return result;
}
int main() {
const char* inputs[] = {"individual", "noe", "objective"};
int n = sizeof(inputs) / sizeof(inputs[0]);
for (int i = 0; i < n; i++) {
printf("Input : %s\n", inputs[i]);
printf("Output: %s\n\n", alternateVowelConsonant(inputs[i]));
}
return 0;
}
Input : individual Output: indiuvidal Input : noe Output: neo Input : objective Output: obejctive
Time and Space Complexity
- Time Complexity: O(n), because we go through the string once to separate characters and once more to build the result.
- Space Complexity: O(n), because we use extra space to store vowels, consonants, and the final result.
Conclusion
We learned to rearrange a string by placing vowels and consonants alternately while keeping their original group order. The approach separates vowels and consonants, then merges them alternately based on which group has more characters.
