X arrange in last by using recursion

 

// Arrange the all x in last.
//input          : "xxaxiixi";
// output      :  "aiiixxx"
#include<iostream>
#include<string>
using namespace std;
 
  string k(string n)
    {  string ans;
   
    if (n.size()==0)
    {return "";}//this ""sign reveal about string.
     if(n[0]=='x')//in comparing always, use sign '' instead of "".
        { ans=(k(n.substr(1))+'x');}
        else { ans=(n[0]+k(n.substr(1)));}
        return ans;}
int main()
{
 
string b="xxaxiixi";
  k(b);
cout<<k(b);
int i=0;
    return 0;
}

Comments

Popular posts from this blog

Write a c++ program to display numbers which is divisible by 2 as well 3 from 6 to n by using and operator .

Write a program to add natural numbers up to 20 by using for loop in c++ language.

Write a c++ program to display numbers which is divisible by 2 as well 3 from 6 to n by using For loop and if else statement .