Reverse Each Word
Aadil has been provided with a sentence in the form of a string as a function parameter. The task is to implement a function so as to print the sentence such that each word in the sentence is reversed.
Example:
Input Format:
Output Format:
Constraints:
Sample Input 1:
Sample Output 1:
Sample Input 2:
Sample Output 2:
#include <iostream>
#include <cstring>
using namespace std;
#include "solution.h"
#include<cstring>
int len(char str[]){
int l=0;
for(int i=0;i!='\0';i++){
l++;
}
return l;
}
void reversetring(char str[], int i ,int j){
while(i<j){
char temp=str[i];
str[i]=str[j];
str[j]=temp;
i++;
j--;
}
}
void reverseEachWord(char str[]) {
int start=0;
int end=0;
int i;
for( i=0;i<strlen(str);i++){
if(str[i]==' '){
end=i-1;
reversetring(str,start,end);
start=i+1;
end=i+1;
}
}
end=i-1;
reversetring(str,start,end);
}
int main() {
int size = 1e6;
char str[size];
cin.getline(str, size);
reverseEachWord(str);
cout << str;
}
Comments
Post a Comment