Hey I'm trying to reverse the words in a string, but I can seem to get it right. I am able to reverse the first word, but not the second.
Here is my strategy -
Reverse the entire string .
Reverse each word in the new string.
This is what I have so far -
Code:
void RevWords(char** str)
{
char temp[255];
int i = 0,j = 0,index;
int len;
StrRev(str);
while(*(*str + i) != '\0')
{
int k =0;
len = GetSpaces((*str + j)) - 1;//returns the length until a space is reached
while(*(*str + j) != ' '){
index = ((*str + i) + len - k) - *str;
temp[j] = *(*str + index);
++j,++k;
}
i += len;
}
temp[i] = '\0';
*str = temp;
}
The output is -
Code:
The original string is hello world.
The revesed string is world╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╬i╠Ω.
Press any key to continue . . .
Thanks!
also, could someone look at my other question as well - Inplace stack reversal(recursive)