Reverse Word Wise

 Reverse Word Wise

Send Feedback

Reverse the given string word wise. That is, the last word in given string should come at 1st place, last second word at 2nd place and so on. Individual words should remain as it is.

Input format :
String in a single line
Output format :
Word wise reversed string in a single line
Constraints :
0 <= |S| <= 10^7
where |S| represents the length of string, S.
Sample Input 1:
Welcome to Coding Ninjas
Sample Output 1:
Ninjas Coding to Welcome
Sample Input 2:
Always indent your code
Sample Output 2:
code your indent Always


#include<bits/stdc++.h>

void reverseStringWordWise(char input[]) {
// Write your code here
int len,i,j,count;
len=strlen(input);
i=0;
while(i<len){
swap(input[i],input[len-1]);
i++;
len--;
}
len=strlen(input);
count=0;
for(i=0;i<=len;i++){
if(input[i]==' ' || input[i]!='\0'){
j=i-1;
while(count<j){
swap(input[count],input[j]);
j--,count++;
}
count=i+1;
}
}
}


#include <iostream>
#include<bits/stdc++.h>

using namespace std;




void reverseStringWordWise(char input[]) {
// Write your code here
int len,i,j,count;
len=strlen(input);
i=0;
while(i<len){
swap(input[i],input[len-1]);
i++;
len--;
}
len=strlen(input);
count=0;
for(i=0;i<=len;i++){
if(input[i]==' ' || input[i]!='\0'){
j=i-1;
while(count<j){
swap(input[count],input[j]);
j--,count++;
}
count=i+1;
}
}
}


int main() {
char input[1000];
cin.getline(input, 1000);
reverseStringWordWise(input);
cout << input << endl;
}

Comments

Popular posts from this blog

Code : All connected components

Coding Ninjas