1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

String - Reverse @ same place in C :-

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

  1. amitajcom

    amitajcom New Member

    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

    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