In: Computer Science
PLEASE PROVIDE COMMENTS ON STEPS
Write a C++ program that modifies a string (null terminated) as follows:
Consonants are positioned at the beginning of the string and vowels are moved to the end of the string.
Example :
Original string : washer
New string : wshrae
Note:
The library string functions cannot be used. You must use pointers and the switch statement to execute this program. Assume that the vowels are a, e, i, o, and u. The modification has to be done in the original char array without creating a new array.
Solution for the given question are as follows -
Code :
#include<iostream>
#include<string.h>
using namespace std;
int main ()
{
// local variable declaration
string str, str1, str2;
// get input from user
cout << "Enter a string : ";
getline (cin, str);
// loop over string
for (int i = 0; str[i]!='\0'; ++i)
{
// check char is vowel or not
switch(str[i]) {
case 'a' :
str1 += str[i];
break;
case 'e' :
str1 += str[i];
break;
case 'i' :
str1 += str[i];
break;
case 'o' :
str1 += str[i];
break;
case 'u' :
str1 += str[i];
break;
default :
str2+= str[i];
break;
}
}
// concate the strings
str = str2 + str1;
// print output
cout << "Final Result : " << str;
return 0;
}
Code screen shot :
Output :