Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   String - Reverse @ same place in C :- (http://www.go4expert.com/forums/string-reverse-c-t30045/)

amitajcom 3Apr2014 04:08

String - Reverse @ same place in C :-
 
how to reverse a string without changing its place in c ?

eg - input: welcome to codechef website
output: emoclew ot fehcedoc etisbew

DRK 7Apr2014 17:16

Re: String - Reverse @ same place in C :-
 
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 = ' ';
        }
    }
}



All times are GMT +5.5. The time now is 01:35.