String - Reverse @ same place in C :-

amitajcom's Avatar, Join Date: Apr 2014
Newbie Member
how to reverse a string without changing its place in c ?

eg - input: welcome to codechef website
output: emoclew ot fehcedoc etisbew
0
DRK's Avatar, Join Date: Apr 2012
DRK
Light Poster
I think you meant rerversing words in a string.
If words are split only by spaces the function would look like this:
Code:
void reverse_words(char *str)
{
    char *end, *ptr = strtok(str, " ");
    while (ptr != NULL)
    {
        strrev(ptr);
        end = ptr + strlen(ptr);
        ptr = strtok(NULL, " ");
        if (ptr != NULL)
        {
            *end = ' ';
        }
    }
}