top of page

write a program to reverse word in a string.



#include <iostream>
//#include<string.h>
#include<string>
using namespace std;
int main()
{
    //char str[100] = {"Hello World"};
    string str = {"Raj is good boy"};
    char temp;
    
    int i, j, k ,len;
    //len = strlen(str);
    len = str.length();
    cout<<len<<endl;
    for(i=0,j=0;j<len;j++)
    {
        if(str[j] == ' ' || (j == len-1))
        {
            if(j < len -1)
            {
                k = j-1;
            }
            else {
                k = j;
            }
            while(i<k)
            {
                temp = str[i];
                str[i] = str[k];
                str[k] = temp;
                i++;
                k--;
            }
            i = j+1;
        }
    }
    cout<<str<<endl;
    return 0;
}

Input: Raj is good boy

output:jar si doog yob

Commentaires


bottom of page