Reversing a string

This is a discussion on Reversing a string within the C Programming forums, part of the General Programming Boards category; I am supposed to reverse an entered string, I was given the hint that you basically copy the string into ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    43

    Reversing a string

    I am supposed to reverse an entered string, I was given the hint that you basically copy the string into another one and then reverse from there but I am not sure how to actually do the reversal. Below is the code I have so far. This succesfully copies the string but im not sure how to reverse, help would be great
    Code:
    # include <stdio.h>
    
    
    
    void strrev (char [], char []);
    
    
    int main ()
    {
        #define lsize 81
        char message[lsize];
        char newmessage [lsize];
        
        printf ("Enter a sentence:  ");
        gets (message);
        
        strrev (newmessage, message);
        
        puts (newmessage);
        
        system ("PAUSE");
        return 0;
    }
    
    
    void strrev (char string1[], char string2[])
    {
         int i=0;
         
         while (string2[i] != '\0')
         {
               string1[i]=string2[i];
               i++;
         }
         string1[i]='\0';
         
         
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Use another variable in the for loop. As i is increasing in one array, the other variables is decreasing (and it started at the highest index of the array, not the lowest index).

    The term 'j' is frequently used for the second variable to i.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    43
    Quote Originally Posted by Adak View Post
    Use another variable in the for loop. As i is increasing in one array, the other variables is decreasing (and it started at the highest index of the array, not the lowest index).

    The term 'j' is frequently used for the second variable to i.
    Ok, I am not sure how to set that up, something like:
    Code:
    void strrev (char string1[], char string2[]){
         int i=0;
         int j;
         
         while (string2[i] != '\0')
         {
               string1[i]=string2[j];
               i++;
         }
         string1[i]='\0';
         
         
    }

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    When you think you have it working, try this string:

    Résumé

    Dino.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  5. #5
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    Quote Originally Posted by Dino View Post
    ... try this string:

    Résumé
    I think it's assumed the strings are composed of ASCII characters (including control characters).
    But the OP din't say so ... so: nice post, Dino!
    Do you have a suggestion on how to deal with your example string?

  6. #6
    Registered User
    Join Date
    Oct 2012
    Posts
    43
    Sorry if I am not too good at this, we just started learning about strings a couple of days ago and our book is quite hard to digest. The entered string is supposed to be a standard sentence from the keyboard such as "Hello world" or "I like pancakes", no special characters and no numbers. I have looked at alot of info online, and most point to doing some kind of for loop but being a beginner with strings I am not sure how to take the hint they gave (copy the string and then start from the back of the string) and implement it into a for loop to reverse the order. I am quite the beginner, so any specific guidance would be great. Thank you all, this is a great forum for people like me

  7. #7
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    You have to look at the characters to determine their encoding. Unicode - Wikipedia, the free encyclopedia

    Dino
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  8. #8
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    If you think about, (well, if I think about it), it's pretty easy.

    A string you get is so long. Create a new variable the same length.
    The first character of the input string goes in the last spot of the new variable
    then, advance one character in your input and subtract one character from your new var, until you have copied strlen(input) characters.

    Dino
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  9. #9
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    As was suggested to use another variable 'j', the point was to go in the opposite direction. While 'i' counts up from zero, j is supposed to start at the end of the string and work backwards. So start by setting j = strlen(string2) - 1;. Then every time i is incremented, decrement j.

    Ignore the smartass posts about handling non-ASCII characters.

  10. #10
    Registered User
    Join Date
    Oct 2012
    Posts
    43
    Quote Originally Posted by nonoob View Post
    As was suggested to use another variable 'j', the point was to go in the opposite direction. While 'i' counts up from zero, j is supposed to start at the end of the string and work backwards. So start by setting j = strlen(string2) - 1;. Then every time i is incremented, decrement j.
    Thank you very much, that was the extra nudge I needed. I ended up with
    Code:
    void strrev (char string1[], char string2[])
    {
         int i=0;
         int j = strlen(string2) - 1;
         
         while (string2[i] != '\0')
         {
               string1[i]=string2[j];
               i++;
               j--;
         }
         string1[i]='\0';
    I also want to thank Adak for getting me on the right track with the 'j' variable, qny and Dino thanks for the extra input as well, it went a little over my head but I hope to keep learning more and more so eventually I can help people on here too. Thanks everyone!

  11. #11
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    383
    Quote Originally Posted by nonoob View Post
    Ignore the smartass posts about handling non-ASCII characters.
    But once you get it working with a single-char encoding like ASCII, it's trivial to change your function to reverse a "wide" string (whose elements are wchar_t instead of char).

  12. #12
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    No it ain't trivial. Requires C++ to start with.

  13. #13
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    Quote Originally Posted by nonoob View Post
    No it ain't trivial. Requires C++ to start with.
    LOL.

    The problem with non-ASCII input is not the language used. The bigger problem is the same: what encoding was used for the input data?
    After that there may or may not be a problem with variable length encodings.

  14. #14
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    383
    Quote Originally Posted by nonoob View Post
    No it ain't trivial. Requires C++ to start with.
    wchar_t and some wide character and wide string functions were introduced in C89. C99 introduced some more wide string functions.

  15. #15
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    383
    Quote Originally Posted by qny View Post
    LOL.

    The problem with non-ASCII input is not the language used. The bigger problem is the same: what encoding was used for the input data?
    After that there may or may not be a problem with variable length encodings.
    Indeed, but I only said it would be a trivial change to the function (reverseString). That function doesn't care about the encoding of user input. The input routines have to worry about the encoding.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reversing a string, again..
    By Disident in forum C Programming
    Replies: 5
    Last Post: 06-15-2009, 09:01 PM
  2. reversing a string
    By swappo in forum C++ Programming
    Replies: 6
    Last Post: 06-14-2009, 04:18 PM
  3. Reversing a string
    By gandamkumar in forum C++ Programming
    Replies: 16
    Last Post: 11-27-2004, 10:09 PM
  4. Reversing a string
    By michelle1 in forum C++ Programming
    Replies: 12
    Last Post: 06-27-2003, 07:37 AM
  5. Reversing a String
    By ToasterPas in forum C++ Programming
    Replies: 10
    Last Post: 08-14-2001, 01:20 PM

Tags for this Thread


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