0
#include<iostream>


using namespace std;

char* Invert(char* input,int start,int end);
char* reversewords(char* current);

int main(){
	char inputstr[] = "London is a city";
	cout << "Inverted string  : " << Invert(inputstr,0,strlen(inputstr)) << endl;
	int start,end ,i;
	while(*inputstr){
		if (*inputstr == ' ' || *inputstr == '\0')
		{
			Invert(inputstr,start,end);
			start = i+1;
		}
		i++;
		end = i;
		(*inputstr++);
	}
	cout << "Inverted string  : " << inputstr << endl;
}

char* Invert(char* input,int start,int end){
	char* current = input;
    int len = strlen(current);
	char temp;
	
	for (int i=start, j=end-1; i<=j; i++,j--)
	{
		temp = *(current+j);
        *(current+j) = *(current+i);
        *(current+i) = temp;
	}
	return input;
}

Above is the code that I've written so far to reverse a string but keep words as it is.
for ex: "London is a City" --> "City a is London"..

Approach that I've followed is
1. Reverse entire string
2. scan character by character, when hit a space or null, reverse that word.

I am getting following error on line 21 : (*inputstr++);
error: lvalue required as left operand of assignment

whats not right in my code? please help!

Edited by keeda: n/a

1
Contributor
2
Replies
3
Views
5 Years
Discussion Span
Last Post by keeda
0

char* Invert(char* input,int start,int end) works as intended.

0

This works perfectly when I replace the while loop by for loop!
whats wrong with my while loop in particular?

for (int k=0;k <= strlen(inputstr) ;k++)
	{
		end = k;
		if (inputstr[k] == ' ' || inputstr[k] == '\0')
		{
			Invert(inputstr,start,end);
			start = k+1;
		}
		
	}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.