Thread: Reversing a String, void style

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    29

    Reversing a String, void style

    Good Evening.
    I am trying to recreate the reverse String function. I realize there is a strrev() that does the same thing, but I wanted to create my own. After running into many problems, I started using the algorithm on programmingsimplified.com. Here is that algorithm
    Code:
    void ReverseString(char* String)  
    {
      int i;
      int length = strlen(String);
      char* fromFront = String;
      char* fromBack = String;
      char temp;
      for (i=0;i<length-1;i++) fromBack++; //move pointer to end of string
      for (i=0;i<length/2;i++) {
          temp = *fromBack;
          *fromBack = *fromFront;
          *fromFront = temp;
          fromBack--;
          fromFront++;
      }
    }
    but when I run my main function to test it, it gives a seg fault when I am assigning *fromfront to *fromback.

    Code:
      int main(int argc, char *argv[])
    {
        char* hi="jewel";
        ReverseString(hi);
        printf ("%s", hi);
        return 0;
    }
    Please help

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Just run(twice ) and it runned and did his job.Are you sure you post the whole code?

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    29
    So this works?,
    Cuz when I run it, it gives a seg fault.

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    This is ridiculous
    Code:
        for (i=0;i<length-1;i++) fromBack++;    //move pointer to end of string
    when this will do
    Code:
        fromBack += length - 1;
    And you don't need i (or length) :
    Code:
    void ReverseString(char* String) {
        char* fromFront = String;
        char* fromBack = String + strlen(String) - 1;
        char temp;
    
        while (fromFront < fromBack) {
            temp = *fromBack; *fromBack = *fromFront; *fromFront = temp;
            fromBack--;
            fromFront++;
        }
    }
    The reason your code doesn't work is because you're calling the routine with a pointer to a string constant. Try calling it like this:
    Code:
    int main(void) {
        char hi[] = "jewel"; // give the string some writable storage
        ReverseString(hi);
        puts(hi);
        return 0;
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    1-You may use printf functions to see where your code crashes.
    e.g
    Code:
    ...
    printf("here1\n");
    here is the line that your project crashes
    printf("here2\n");
    ...
    output will be
    Code:
    here1
    2-i would suggest to write your own code,not a copy paste one.
    Hint- reverse the string character by character
    post back -of course- if needed

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by oogabooga View Post
    This is ridiculous
    Code:
        for (i=0;i<length-1;i++) fromBack++;    //move pointer to end of string
    when this will do
    Code:
        fromBack += length - 1;
    Are you sure?

    Just run it on Linux and i got a seg fault too
    Last edited by std10093; 08-22-2012 at 05:51 PM.

  7. #7
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    You can't write to a string constant. Change this:
    Code:
        char* hi="jewel";
    to this:
    Code:
        char hi[]="jewel";
    Edit: too slow. oogabooga beat me to it.
    Edit: std10093: yes, it's basic pointer arithmetic. you're getting a segfault because it's trying to write to a string constant.
    Last edited by christop; 08-22-2012 at 05:57 PM.

  8. #8
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by christop View Post
    Edit: std10093: yes, it's basic pointer arithmetic. you're getting a segfault because it's trying to write to a string constant.
    I do not think i understand fully what the problem is.Can someone perhaps explain?

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    45
    Quote Originally Posted by std10093 View Post
    I do not think i understand fully what the problem is.Can someone perhaps explain?
    What it means is that u cannot rearrange the characters of a string when u declare it as a constant like char *str = "some_string". U can however rearrange the characters when u save the string as an array of characters eg char str[] = some_string

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Code:
    char *str = "jewel";
    That declares str as a pointer. It initializes str to point to the place in memory where the string constant "jewel" is contained. Since "jewel" is used as a string constant, it exists in a read-only segment of memory. That means, that if your code tries to write to a read-only segment of memory, it violates the rules for that segment, hence the "segmentation violation" or "seg fault".

    Code:
    char arr[] = "jewel";
    That declares arr as a char array of length 6 (5 for the letters in "jewel" + 1 for the null terminator). The array exists on the stack (if declared locally) or in global variable space (if declared globally), and thus is in a writable segment of memory, hence reversing it in place is allowed and doesn't cause a seg fault.

    @std10093:
    The reason this probably "worked" for you the first time is not a Linux/non-Linux issue. It is probably because you were using an old compiler (Turbo C or Dev-C++ perhaps?). Old compilers often used to place string constants in writable memory, but that meant the program could change the contents, and they weren't exactly constant any more. Try upgrading your other compiler.

  11. #11
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by livin View Post
    What it means is that u cannot rearrange the characters of a string when u declare it as a constant like char *str = "some_string". U can however rearrange the characters when u save the string as an array of characters eg char str[] = some_string
    You are right thank you

    Quote Originally Posted by anduril462 View Post
    Code:
    char *str = "jewel";
    Tottaly agree. I had forgotten so thanks.
    Quote Originally Posted by anduril462 View Post
    @std10093:
    The reason this probably "worked" for you the first time is not a Linux/non-Linux issue. It is probably because you were using an old compiler (Turbo C or Dev-C++ perhaps?). Old compilers often used to place string constants in writable memory, but that meant the program could change the contents, and they weren't exactly constant any more. Try upgrading your other compiler.
    I have netbeans the latest version,so the compiler may also be updated.

  12. #12
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    If using GCC, the command line option "-Wwrite-strings" will catch this for you.

    gg

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, 08:01 PM
  2. reversing a string
    By swappo in forum C++ Programming
    Replies: 6
    Last Post: 06-14-2009, 03:18 PM
  3. Reversing a String in C
    By Ice in forum C Programming
    Replies: 6
    Last Post: 04-18-2002, 10:55 AM
  4. c-style string vs. c++-style strings
    By Mbrio in forum C++ Programming
    Replies: 3
    Last Post: 02-10-2002, 12:26 PM
  5. Reversing a String
    By ToasterPas in forum C++ Programming
    Replies: 10
    Last Post: 08-14-2001, 12:20 PM

Tags for this Thread