Thread: Reverese a String without loop

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    2

    Question Reverese a String without loop

    Using C, how to write a program to reverse a string without using loop and recursion.
    Last edited by sultan raja; 11-09-2003 at 04:41 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    hmm... could you clarify what you mean?

    You're asking for an algorithm to reverse the characters of an arbitrary string, without using iteration or recursion?

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    2
    Originally posted by laserlight
    hmm... could you clarify what you mean?

    You're asking for an algorithm to reverse the characters of an arbitrary string, without using iteration or recursion?
    Yes.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by sultan raja
    Yes.
    Welcome to reality. No one here is going to do your home work. Perhaps you should actually read the forum rules?

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

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Easy money - though I don't think you're going to like it.

    #include <std_pause_for_dramatic_effect.h>
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Well, you can use strrev from string.h if your compiler supports it. But that's probably not what you had in mind
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  7. #7
    Registered User
    Join Date
    Nov 2003
    Posts
    5
    If the string is always the same length there is a way to code it yes.

    If the string is different lengths each time I'm not experienced enough to help you.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >without using loop and recursion
    How are you allowed to define loops?
    My best code is written with the delete key.

  9. #9
    Registered User
    Join Date
    Oct 2003
    Posts
    27

    Re: Reverese a String without loop

    Originally posted by sultan raja
    Using C, how to write a program to reverse a string without using loop and recursion.
    what is the string?

    I'm new to this stuff too, but I have done some exercises with reversing digits, which could be done using remainder operator (%) or you could try this:

    printf("%d%d%d", a, b ,c)

    reversed:
    printf("%d%d%d", c, b, a)

    no loop and recursion used i believe...

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Is it safe to post my answer yet....

    safe = homework deadline passed
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main () {
        char    string[100];
        size_t  len;
        scanf("%s",string);
        len = strlen( string );
        switch ( len ) {
            case 10:   putchar( string[len--] );
            case 9:    putchar( string[len--] );
            case 8:    putchar( string[len--] );
            case 7:    putchar( string[len--] );
            case 6:    putchar( string[len--] );
            case 5:    putchar( string[len--] );
            case 4:    putchar( string[len--] );
            case 3:    putchar( string[len--] );
            case 2:    putchar( string[len--] );
            case 1:    putchar( string[len--] );
            case 0:    putchar( string[len  ] );
        }
        putchar( '\n' );
        return 0;
    }
    Not the most elegant, but at least no obvious loops
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    of course strlen has a loop in it...
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The problem is with the "arbitrary string" part.
    We dont know how long the string will be.
    If we did know, we can manually code it in.

    I wonder if sultan raja was given a trick question and misinterpreted it.

    After all, it is possible to reverse an arbitrary string using iteration XOR recursion, thus satisfying not using iteration AND recursion


  14. #14
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    are you allowed to use goto?


  15. #15
    Registered User
    Join Date
    Nov 2003
    Posts
    5
    What about about using a stack.

    Push a letter down on the stack every letter that gets entered.

    When input is done just read the stack from the top to bottom.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  2. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  3. Scope And Parameter Passing
    By djwicks in forum C Programming
    Replies: 6
    Last Post: 03-28-2005, 08:26 PM
  4. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM
  5. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM