Reverse Each Word
Reverse Each Word
Send Feedback
Reverse Each Word
Input Sentence: "Hello, I am Aadil!"
The expected output will print, ",olleH I ma !lidaA".
The first and only line of input contains a string without any leading and trailing spaces. The input string represents the sentence given to Aadil.
The only line of output prints the sentence(string) such that each word in the sentence is reversed.
0 <= N <= 10^6
Where N is the length of the input string.
Time Limit: 1 second
Welcome to Coding Ninjas
emocleW ot gnidoC sajniN
Always indent your code
syawlA tnedni ruoy edoc
#include<cstring>using namespace std;void reverseStringWordWise(char input[]) { int len,i,j,count; char temp; len=strlen(input); i=0; while(i<len) { temp=input[i]; input[i]=input[len-1]; input[len-1]=temp; 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) { temp=input[count]; input[count]=input[j]; input[j]=temp; count++; j--; } count=i+1; } } }
#include <iostream>#include <cstring>using namespace std;
using namespace std;void reverseStringWordWise(char input[]) { int len,i,j,count; char temp; len=strlen(input); i=0; while(i<len) { temp=input[i]; input[i]=input[len-1]; input[len-1]=temp; 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) { temp=input[count]; input[count]=input[j]; input[j]=temp; count++; j--; } count=i+1; } } }
int main() { int size = 1e6; char str[size]; cin.getline(str, size); reverseEachWord(str); cout << str;}
Comments
Post a Comment