Reverse Word Wise
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 :
Output format :
Constraints :
Sample Input 1:
Sample Output 1:
Sample Input 2:
Sample Output 2:
#include <iostream>
#include "solution.h"
using namespace std;
#include<string.h>
int lenght(char str[]){
int count=0;
for (int i=0;str[i]!='\0';i++){
count++;
}
return count;
}
void reverseStringWordWise(char str[]) {
{
int length = strlen(str);
// Traverse string from end
int i;
for (i = length - 1; i >= 0; i--) {
if (str[i] == ' ') {
// putting the NULL character at the
// position of space characters for
// next iteration.
str[i] = '\0';
// Start from next charatcer
printf("%s ", &(str[i]) + 1);
}
}
// printing the last word
}
}
int main() {
char input[1000];
cin.getline(input, 1000);
reverseStringWordWise(input);
cout << input << endl;
}
https://www.geeksforgeeks.org/print-words-string-reverse-order/
#include <iostream>
// Write your code here
int len=lenght(input);
int i=0;
int j=len-1;
while(i<j){
char temp=input[i];
input[i]=input[j];
input[j]=temp;
i++;
j--;
}
i=0;
j=0;
for(i=0;i<lenght(input);i++){
if(input[i]==' '){
while((i-1)>j-1){
char temp=input[i-1];
input[i-1]=input[j-1];
input[j-1]=temp;
i--;
j++;
}
j=j+i+1;
i=j;
}
int main() {
// Write C++ code here
std::cout << "Hello world!";
return 0;
}
Comments
Post a Comment