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".
A:
Shimon
i wanted to show you a wonderful solution for reversing a string:
void REVERSE()
{
char line[MAXLINE];
cin>>line; //'line' gets the word untill the user insert space(or return).
char word;
cin.get(word); //gets the first character after the word.
if(word!='\n') //the user did not insert enter.
REVERSE(); //calling the function
cout<
}
amazing ah?
A:
Yair
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[0]= 'The last space in the string' A[1]=...
A:
kneofyte
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.
#include
int main()
{
char str[]="This is a string";
int index,length;
char *first, *last;
for(index=0;str[index]!='\0';index++)
if(str[index]==32)
str[index]='\0';
index--;
last=&str[index];
while(last>=str)
{
while(*last!='\0')
last--;
last++;
printf("%s",last);
printf(" ");
last--;
last--;
}
}