Thread: flip

  1. #1
    Unregistered
    Guest

    Unhappy flip

    I am learning C and I am trying to flip a string that contains
    "hello" into "olleh"

    how can I do that?

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    There are many ways.... why not try and think of some yourself first

    Remember that the string is just an array of chars......
    And remember that many people have asked the question before you, so the answer is going to be lying around somewhere.

    Have a go, and post some code when you are stuck.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    start at the end of the string and work your way towards the beginning, printing off characters as you go.

  4. #4
    Banned borko_b's Avatar
    Join Date
    Jun 2002
    Location
    Well... I live in Bulgaria :)
    Posts
    100
    Code:
    int flip_str_to_str(char *in,char *out) {
         char *pin,*pout;
         int len = strlen(in) ;
    
         pin = in ;
         pout = out+len; 
         *(pout+1) = '\0';
         while( *pin) {
              *pout = *pin;
              pout--;
              pin++;
         }
         return 0;
    }
    Just make sure that 'in' buffer is long as 'out' buffer
    or else out is going to ring the bell !!

  5. #5
    Unregistered
    Guest
    Is there a way to do it in a much simpler way..I just started to learn C soPin iin/out is too advanced for me now!!!!

    I want to find a way to rotate the string without having printf to do it!!!

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    void r(char*s)
    {
        if(s&&*s!='\0')
        {
            r(s+1);
            printf("%c",*s);
        }
    }
    If this is homework, they'll likely not accept this answer, unless of course, you can actually explain what it does.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Akilla
    Guest

    Here's my version..

    Haven't checked this one... (there could be mistakes
    with strlen in my code..)
    Code:
    char input[] = "My Homework";
    for (i=strlen(input)-1; i>=0; i--)
    {
         printf("%c", input[i]);
    }
    This is just to give you an idea.

    COOL PROGRAMS @ www.akilla.tk

  8. #8
    Akilla
    Guest

    Exclamation oh..

    oh, and also
    int i
    and all that..
    Code:
    char ISay[] = "Can't expect me to do all your homework";
    Good luck

    CHECK OUT MY NUMEROLOGY PROGRAM
    (Tells Lucky days, Lucky Colors, etc.) www.akilla.tk

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Does memcpy cause endian flip?
    By Subsonics in forum C Programming
    Replies: 7
    Last Post: 01-17-2009, 06:30 PM
  2. flip
    By bennyho03 in forum C Programming
    Replies: 18
    Last Post: 09-10-2004, 05:55 AM
  3. code doesnt flip array
    By noob2c in forum C Programming
    Replies: 3
    Last Post: 03-29-2003, 09:54 AM
  4. Need help on flip array algorithm
    By DiepVien_007 in forum C Programming
    Replies: 4
    Last Post: 03-17-2003, 04:59 PM
  5. C++ algorithm help
    By Gene126 in forum C++ Programming
    Replies: 8
    Last Post: 10-10-2002, 03:17 PM