Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a c++ program that modifies a null terminated string of lowercase letters

ID: 670476 • Letter: W

Question

Write a c++ program that modifies a null terminated string of lowercase
letters as follows: Consonants are moved to the beginning of the string and
vowels are moved to the end of the string. Modifications will preserve the relative
order of the consonants within themselves and similarly relative order of the vowels
among themselves.
Example : Original string : pencil Modified string : pnclei
Original string : river Modified string : rvrie
Note: The program should be implemented with pointers. Assume that the
vowels are a, e, i, o, and u. The modification has to be done in the original
character array without creating a new array. Further, the library string
functions cannot be used in the solution of this problem.Write a program that modifies a null terminated string of lowercase
letters as follows: Consonants are moved to the beginning of the string and
vowels are moved to the end of the string. Modifications will preserve the relative
order of the consonants within themselves and similarly relative order of the vowels
among themselves.
Example : Original string : pencil Modified string : pnclei
Original string : river Modified string : rvrie
Note: The program should be implemented with pointers. Assume that the
vowels are a, e, i, o, and u. The modification has to be done in the original
character array without creating a new array. Further, the library string
functions cannot be used in the solution of this problem.

Explanation / Answer

#include <iostream>

#include <string>

using namespace std;

int getNextConsonant(string a,int pos)

{

int i = pos;

while(a[i])

{

if(a[i] != 'a' && a[i] != 'e' && a[i] != 'i' && a[i] != 'o' && a[i] != 'u' && a[i] != 'A' && a[i] != 'E' && a[i] != 'I' && a[i] != 'O' && a[i] != 'U')

{

return i;

}

i = i+1;

}

  

return 0;

}

int main() {

  

  

string a = "Hello";

int temp ,i=0;

char c;

  

while (a[i]) {

  

if (a[i] == 'a' || a[i] == 'e' || a[i] == 'i' || a[i] == 'o' || a[i] == 'u' || a[i] == 'A' || a[i] == 'E' || a[i] == 'I' || a[i] == 'O' || a[i] == 'U')

{

temp = getNextConsonant(a,i+1);

if(temp!=0)

{

c = a[temp];

a[temp] = a[i];

a[i] = c;

  

  

}

else

break;

  

}

i = i+1;

}

  

cout << a;

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote