Thread: How would you recommend to reverse a string?

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    24

    Question How would you recommend to reverse a string?

    Hi. For part of a code I'm making, I want to reverse strings.

    .sgnirts esrever ot tnaw I ,gnikam m'I edoc a fo trap roF .iH


    What I was thinking of doing is making a kind of bubblesort function for strings, but that seems like it would take a lot of code. My professor has said this can be done in about ten lines of code. I figure I'll need a for loop and I'll need an integer denoting the number of characters in the string.

    Eh, what would be the best method of going about this?(I'm just looking for a few hints).

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Well, if you want to give your professor a chuckle, you could do it recursively

    However, the good way to do it is something like this...
    Your integer you need to make index the last character in the string. Then you need the for loop to print the character that i is indexing, decrease i, and repeat untill i indexes the first character.
    Code:
    int i;
    char * S;
    make S[i] be end of string
    decrease i
    print S[i]
    if i != 0, repeat
    Callou collei we'll code the way
    Of prime numbers and pings!

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    24
    Originally posted by QuestionC
    Well, if you want to give your professor a chuckle, you could do it recursively

    However, the good way to do it is something like this...
    Your integer you need to make index the last character in the string. Then you need the for loop to print the character that i is indexing, decrease i, and repeat untill i indexes the first character.
    Code:
    int i;
    char * S;
    make S[i] be end of string
    decrease i
    print S[i]
    if i != 0, repeat
    So I imagine i would want to make a loop like

    for (i = strlen(S); i != 0; --i)
    {
    printf s[i];(I know that's not how the real code goes)
    }

    right?

    Wow, that was incredibly easy.

    The pre increment(as opposed to i--) would make sure the '\0' character is not touched, and also make sure that the i = 0 position would be done before the loop checked it and ended, right?

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    24
    Code:
    #include <stdio.h>
    #include <string.h>
    #define SIZE 1024
    
    int main()
    {
    	char s[SIZE];
    	int i;
    	
    	gets(s);
    	for (i = strlen(s); i != 0; --i) 
    	{ 
    		printf ("\n\n%s", s[i]); 
    	} 
    }
    DAMN! Compiles fine but stack dump violations all OVER my face! What stack dump am I violating?

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    printf ("\n\n%s", s[i]);

    s is a string
    s[i] is a char

    use this instead...
    printf("%c", s[i]);

    actually, you can also try using this command...
    putchar(s[i]);

    it should do the same thing.

    I hope that works, as it is, you're printing strings which start at various places in memory.
    Callou collei we'll code the way
    Of prime numbers and pings!

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Recursivly:

    void revstring( char *s ) { if (s) revstring( s+1 ); putc( s ); }

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

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    21
    I believe that code misses the first character of the string, to fix that you might do something like

    for (i = strlen(s); i >= 0; --i)

    you're also missing the newline character, but I assume you do that purposely because it comes out ugly if you don't (:

    I'm wrong a lot,

    (: Ian

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    I should prolly point out too that in the for loop, your
    --i
    isn't going to do anything different from
    i--
    Since that statement is going to be evaluated after the body of the loop. There is a little trick you can do however...
    Code:
    for (i = strlen(s); i != 0;)
    	{
    		printf ("\n\n%s", s[--i]);
    	}
    Clever, no?
    Callou collei we'll code the way
    Of prime numbers and pings!

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    24
    Ok, now it's almost working.

    Code:
    #include <stdio.h>
    #include <string.h>
    #define SIZE 1024
    
    int main()
    {
    	char s[SIZE];
    	int i;
    	printf("Enter a string:\n");
    	gets(s);
    	printf("\nThe string in reverse print: ");
    	for (i = strlen(s); i >= 0; --i) 
    		printf ("%c", s[i]); 
    }
    The only problem.

    Output of program(output in bold, user input in italics):
    Enter a string:
    I will enter a string

    The string in reverse print: 00 gnirts a retne lliw I
    What's up with the zero zero at the beginning?


    Edit: I fixed it by setting i = strlen - 1 but I still don't get why that 00 was at the front earlier.
    Last edited by goodn; 11-13-2001 at 07:23 PM.

  10. #10
    Registered User Twiggy's Avatar
    Join Date
    Oct 2001
    Posts
    43
    I'm no C programming expert by any means. But if I were you i'd make a seperate fuction for the get input. Declare the variables outside of both functions, then just do a

    void do_get_input()
    {
    printf("Yadda yadda");
    scanf("%s", whatever);
    }

    might help I dunno. :-)
    To error is human, to really foul things up requires a computer

  11. #11
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    With the 00...

    I'm not sure if you understand how strings are stored in memory, but it's basically an array. In order to keep track of how long each string is, a special character is kept at the end of each string, '\0'. Now, '\0' is not a valid printing character, so what I figure is that your compiler prints 00 when it encounters the '\0' character. An easy way to test this...
    Code:
    int main()
    {
     printf ("%c", '\0');
     return 0;
    }
    Apparantly mine doesn't print anything for this.
    Callou collei we'll code the way
    Of prime numbers and pings!

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    24
    I knew about \0, bt I was under the assumption I would skip this character thanks to my pre-increment. Oh well, the program works perfectly now anyway.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  2. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  4. reverse string
    By b-ref in forum C Programming
    Replies: 2
    Last Post: 11-25-2001, 09:41 AM