top of page

write a program to reverse the string in same position


Input:Hello i am in bangalore


output:olleH i ma ni erolagnab


Input:Hello i am in bangalore


output:bangalore in am i Hello




// Example program


#include <iostream>
#include <string>
using namespace std;

string stringRev(string name,int n)
{
    string temp = name;
    cout<<temp<<endl;
    int j=0;
    for(int i = n-1;i>=0;i--)
    {
        temp[j] = name[i];
        j++;
    }
    
    return temp;
}


string stringNext(string str1, int n1)
{
    string  temp1 = str1;
    cout<<"stringNext:"<<temp1<<endl;
    int k=0,l=0;
    for(int i = 0; i<=n1 ;i++)
    {
        //cout<<"In for loop"<<endl;
        if(str1[i] == ' ' || str1[i] == '\0')
        {
            //cout<<"In if condn"<<endl;
            for(int j=i-1;j>=k;j--)
            {
                //cout<<"In second for loop"<<endl;
                temp1[l] = str1[j];
                l++;
            }\
            k=i+1;
            temp1[l++] = ' ';
        }
    }
    cout<<"last String is:"<<temp1<<endl;
    return temp1;
}

int main()
{
    string name;
    cout<<"Enter the Name:"<<endl;
    getline (cin, name);
    
    int n = name.length();
    
    //string rstring = stringRev(name,n);
    
    //cout<<rstring<<endl;
    
    string nxtstring = stringNext(name,n);
    cout<<nxtstring<<endl;
    return 0;
}

Comments


bottom of page