Advance Reverse string

This is a discussion on Advance Reverse string within the C Programming forums, part of the General Programming Boards category; Hi there all, Sorry if i post another reverse string. Really need all your help. I am trying to reverse ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    33

    Advance Reverse string

    Hi there all, Sorry if i post another reverse string. Really need all your help.

    I am trying to reverse a string "HELLO" but i need the end result to be like this " OLLEHHELLO" a mirror effect. I cannot use string.h.

    Only can use recursion.

    I tried modifying some codes on reversing but all failed. Please help

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Reverse the string, then concatenate the normal string with the reversed string using strcpy() and strcat().
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    strm100
    Guest
    I cannot use the strcpy function or strrev function

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    The reverse function is easily done using a for loop and a swap-value function (which you write).

    You can also do the concatenate function yourself using a for loop (or two).

    I have no idea how you could do it easily with recursion, since you cannot return a string (are not allowed to use string objects).
    Code:
    void Swap(char* C1, char* C2)
    {
       char Temp = *C1;
       *C1 = *C2;
       *C2 = Temp;
    }
    
    void Reverse(char* String)
    {
       int Length = strlen(String);
       for(int i=0; i<(Length/2); i++)
       {
          Swap(&String[i], &String[Length - i - 1]);
       }
    }
    
    void Concatenate(char* Buffer, char* S1, char* S2)
    {
       int i;
       int L1 = strlen(S1);
       int L2 = strlen(S2);
    
       for(i=0; i<L1; i++)
       {
          Buffer[i] = S1[i];
       }
       for(i=0; i<L2; i++)
       {
          Buffer[L1 + i] = S2[i];
       }
       Buffer[L1 + L2] = '\0';
    }
    
    int main()
    {
       char Buffer[256];
       char StrHello[16] = "Hello";
       char StrHelloReverse[16] = "Hello";
    
       Reverse(StrHelloReverse);
       Concatenate(Buffer, StrHello, StrHelloReverse);
       cout << Buffer;
    
       return 0;
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by Salem
    Mmm, Magos with the C++
    Hmmm... I rarely check if I'm posting in the C or C++ forum, and since I like cout better than printf I usually use it... .
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  6. #6
    Waiting_11
    Guest

    Wink a better solution

    #include <stdio.h>

    #define MAX_BUFFER_SIZE 256

    char buf[MAX_BUFFER_SIZE] = {0};
    int i = 0;

    char* reverse(char *s)
    {
    char *p;

    for (p = s; *p != '\0'; p++)
    ;

    for (p--; p != s; p--)
    buf[i++] = *p;

    buf[i++] = *p;

    return (char *)&buf[0];
    }

    main()
    {
    char *s = "HELLO";

    reverse(reverse(s));

    printf("%s\n", buf);
    }

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826

    Re: a better solution

    Originally posted by Waiting_11

    char buf[MAX_BUFFER_SIZE] = {0};

    return (char *)&buf[0];
    1) Why are you returning a global?
    2) What's with the horrible cludge of a return statement?

    This is so much nicer:

    return buf;

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 03:45 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Reverse a string (without using any string functions?)
    By geetard in forum C Programming
    Replies: 2
    Last Post: 11-15-2006, 07:42 PM
  4. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 04:58 PM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21