#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!

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

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;
		}
		
	}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.