Q:Write a function to reverse the words in the sentence,
for example "This is a string " becomes "string a is This".
The memory is limited. You can't use another string for replacement .
A:Yung G Chen
One possible solution - Write a string reversal routine,
and pass the entire string to it, so that "This is a string"
becomes "gnirts a si sihT" .
Then pass individual word to the above routine, (with the help of strtok),
so that "gnirts" becomes "string", "a" becomes "a", "si" becomes "is"
and "sihT" becomes "This".
i wanted to show you a wonderful solution for reversing a string:
cin>>line; //'line' gets the word untill the user insert space(or return).
cin.get(word); //gets the first character after the word.
if(word!='\n') //the user did not insert enter.
REVERSE(); //calling the function
U have to use array of char* that will point to start and end
of the substrings given in the initial string.
The new char* array say A, will have the value for instance
A= 'The last space in the string' A=...
Solution: Populate the input string with NULL as delimiter for each substring and use a character
pointer to traverse to the start of each substring in the reverse and print out the substrings.
char str="This is a string";
char *first, *last;