In: Computer Science
PLEASE PROVIDE COMMENTS ON STEPS
IMPORTANT NOTE:
The library string functions cant be used. You must use pointers and the switch statement to execute this program. Assume that the vowels are a, e, i, o, u. The modification has to be done in the original char array without creating a new array.
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
#include <iostream> #include <string> using namespace std; int main() { string s; cout<<"Original string: "; cin>>s; int len = s.length(); char* ptr = new char[s.length()]; for(int i = 0;i<=len;i++){ ptr[i] = s[i]; } int count = 0; char ch; // Loop through all the elements of string s for(char* p = ptr; p<(ptr+len-count);p++){ ch = *p; // Checking for a vowel switch(ch){ case 'a': case 'e': case 'i': case 'o': case 'u': // Moving all the characters from index i+1 to one index left // This makes last position empty for vowel ch for(char* q = p+1;q<(ptr+len);q++){ *(q-1) = *q; } // Replacing last character with ch *(ptr+len-1) = ch; // Incrementing count count++; break; } //cout<<(*(ptr))<<end; } cout<<"New string: "<<ptr<<endl; return 0; }