Thread: Reverse string question - for loop

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    34

    Reverse string question - for loop

    Could someone please explain exactly what the for statement in this problem is doing step by step?

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
        char string[100], reversestring[100];
        int x, y;
        
        printf("Enter the string to reverse:");
        scanf("%s", string);
        
        for (i=strlen(string)-1, x=0; i>=0; x--, y++){
            reversestring[y]=string[x]; }
        
        reversestring[y]='\0';
        
        printf("Your string %s reversed is %s \n", string, reversestring);
    
    
    }

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Smells like a homework question. Give it a shot first and then we'll tell you how you're doing.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by benrogers View Post
    Could someone please explain exactly what the for statement in this problem is doing step by step?
    It's reversing the string named "string", char by char.

    Add a line of code to print string[x], and x, and see what it prints out. Use %c for string[x] and %d format for x, since it is an integer.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    It actually does a whole lot of nothing, but I'll let the OP take a crack at exactly why that is.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by benrogers View Post
    Could someone please explain exactly what the for statement in this problem is doing step by step?

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
        char string[100], reversestring[100];
        int x, y;
        
        printf("Enter the string to reverse:");
        scanf("%s", string);
        
        for (i=strlen(string)-1, x=0; i>=0; x--, y++){
            reversestring[y]=string[x]; }
        
        reversestring[y]='\0';
        
        printf("Your string %s reversed is %s \n", string, reversestring);
    
    
    }
    Why don't you run it in a debugging utility and find out for yourself...

    Hint: It actually does a whole lot of nothing.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by CommonTater View Post
    Hint: It actually does a whole lot of nothing.
    Too bad your hint is wrong. y is used uninitialized, so there is a really good chance that all this code just segfaults.


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

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    Too bad your hint is wrong. y is used uninitialized, so there is a really good chance that all this code just segfaults.
    Quzah.
    That and i is never decremented... letting x and y run wild...

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That's two things I haven't noticed today. I need about five more cups of coffee.


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

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    that's two things i haven't noticed today. I need about five more cups of coffee.
    Attachment 10410
    Last edited by CommonTater; 03-16-2011 at 11:52 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to NOT reverse a string in place.
    By cdalten in forum C Programming
    Replies: 2
    Last Post: 01-12-2006, 09:06 AM
  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. Next Question...
    By Azmeos in forum C++ Programming
    Replies: 3
    Last Post: 06-06-2003, 02:40 PM