Problem Statement You are given a function, char* VowelConsonantSequence(char* str); The function takes a string 'str' as input. All consecutive consonants are replaced by a single letter 'C' and all consecutive vowels are replaced by a single letter 'V', such that output string does not have two or more consecutive 'C' or 'V'. Implement the function to generate this string of alternate 'C' and 'V' and return the same. Note: str will contain only lower case English alphabets. The vowels in the English alphabets are 'a', 'e', 'i', 'o' and 'u'. All the other alphabets are consonants. Output string should have only upper-case letters 'C' and 'V'. Example: Input: whereabouts Output: CVCVCVC Explanation: Here, the sequence in the input string is consonant-consonant-vowel-consonant-vowel-vowel-consonant-vowel-vowel-consonant-consonant. Putting together consecutive consonants and vowels gives us CVCVCVC. Note that the sequence has been preserved along with the constraint that no two "V"s or "C"s appear together. Sample input yoghurt Sample Output CVCVC Instructions : This is a template based question, DO NOT write the "main" function. Your code is judged by an automated system, do not write any additional welcome/greeting messages. "Save and Test" only checks for basic test cases, more rigorous cases will be used to judge your code while scoring. Additional score will be given for writing optimized code both in terms of memory and execution time.
Question
Problem Statement You are given a function, char* VowelConsonantSequence(char* str);
The function takes a string 'str' as input. All consecutive consonants are replaced by a single letter 'C' and all consecutive vowels are replaced by a single letter 'V', such that output string does not have two or more consecutive 'C' or 'V'. Implement the function to generate this string of alternate 'C' and 'V' and return the same. Note: str will contain only lower case English alphabets. The vowels in the English alphabets are 'a', 'e', 'i', 'o' and 'u'. All the other alphabets are consonants. Output string should have only upper-case letters 'C' and 'V'.
Example: Input: whereabouts Output: CVCVCVC Explanation: Here, the sequence in the input string is consonant-consonant-vowel-consonant-vowel-vowel-consonant-vowel-vowel-consonant-consonant. Putting together consecutive consonants and vowels gives us CVCVCVC. Note that the sequence has been preserved along with the constraint that no two "V"s or "C"s appear together. Sample input yoghurt Sample Output CVCVC Instructions : This is a template based question, DO NOT write the "main" function. Your code is judged by an automated system, do not write any additional welcome/greeting messages. "Save and Test" only checks for basic test cases, more rigorous cases will be used to judge your code while scoring. Additional score will be given for writing optimized code both in terms of memory and execution time.
Solution
To solve this problem, you need to iterate over the string and check each character. If the character is a vowel, replace it with 'V'. If it's a consonant, replace it with 'C'. However, you also need to check the previous character to ensure that there are no consecutive 'V's or 'C's. Here is a step-by-step guide on how to implement this in C:
-
Define a function with the signature
char* VowelConsonantSequence(char* str). This function takes a string as input and returns a string. -
Inside the function, create a variable
resultto store the final string. Also, create a variableprevto store the previous character. -
Iterate over the string using a for loop. For each character, do the following:
a. If the character is a vowel (i.e., 'a', 'e', 'i', 'o', 'u'), check if the
prevcharacter is not 'V'. If it's not, append 'V' toresultand setprevto 'V'.b. If the character is a consonant, check if the
prevcharacter is not 'C'. If it's not, append 'C' toresultand setprevto 'C'. -
After the loop, return
result.
Here is a sample implementation in C:
#include <string.h>
#include <stdlib.h>
char* VowelConsonantSequence(char* str) {
char* result = (char*)malloc(strlen(str) + 1);
char prev = '\0';
int j = 0;
for (int i = 0; str[i] != '\0'; i++) {
if (str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u') {
if (prev != 'V') {
result[j++] = 'V';
prev = 'V';
}
} else {
if (prev != 'C') {
result[j++] = 'C';
prev = 'C';
}
}
}
result[j] = '\0';
return result;
}
This code works by iterating over the string and checking each character. If the character is a vowel and the previous character is not 'V', it appends 'V' to the result. If the character is a consonant and the previous character is not 'C', it appends 'C' to the result. This ensures that there are no consecutive 'V's or 'C's in the result.
Similar Questions
Write a function that takes a string as input and returns a new string with all the vowels removed.
Write a C program to check whether an alphabet is vowel or consonant using switch case
Take character as input from the console using input() function. Write a program to check whether the given input is a vowel or a consonant or a letter are not. Print the result to the console as shown in the examples.Sample Input and Output 1:ch: Aletter and vowelSample Input and Output 2:ch: Pletter and consonantSample Input and Output 3:ch: @not letter
Write a program in C to count the number of vowels and consonants in a string. You can use fgets to read the characters.
Write a program that will take one string as input. The program will then remove vowels a, e, i, o, u (in lower or upper case) from the string. If there are two or more than two vowels that occur together then the program shall ignore all of those vowels.Sample input Cat Sample output CtSample input HeelSample output Heel
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.