1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

String - Reverse @ same place in C :-

Discussion in 'C' started by amitajcom, Apr 2, 2014.

  1. amitajcom

    amitajcom New Member

    Joined:
    Apr 2, 2014
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    India,bangalore
    how to reverse a string without changing its place in c ?

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

    DRK New Member

    Joined:
    Apr 13, 2012
    Messages:
    44
    Likes Received:
    3
    Trophy Points:
    0
    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 = ' ';
            }
        }
    }
     

Share This Page