Easy way to reverse a string

This is a discussion on Easy way to reverse a string within the C Programming forums, part of the General Programming Boards category; As I showed in a recent post, cute XOR tricks to swap a pair of values are likely to make ...

  1. #16
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    As I showed in a recent post, cute XOR tricks to swap a pair of values are likely to make things worse rather than better.

    > s = reverse_str("mango");
    > output > segmentation fault
    > why ??
    "strings" can be placed in read-only memory (you can't change read-only memory)
    As soon as you try and swap a pair of characters, its game over for you

    char a[] = "mango";
    s = reverse( a );
    should work, if reverse() does as advertised.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  2. #17
    Slime Dragoon_42's Avatar
    Join Date
    Feb 2003
    Location
    Vancouver
    Posts
    90
    Thanks for all the help everyone! I had to post to tell Salem that I love you avatar.

  3. #18
    Registered User
    Join Date
    Dec 2003
    Posts
    92
    "strings" can be placed in read-only memory (you can't change read-only memory)
    hmmm......i did a small experiment.....

    code-1
    ---------

    Code:
    int main()
    {
    char *s ="mango";
    s[3] ='p';  // read only..so no change is allowed.
    cout<<s<<endl;
    
    }
    output >segmentation fault.




    code-2
    ----------
    Code:
    char *s =new char[6]; // giving some memory
    s="mango";
    s[3] ='p';
    cout<<s<<endl;

    code-2 is also giving segmentaion fault !!
    i have given memory to the code -2 by new operator to play around. how it still remains read only . why the latter case not read-write both !!.....does it mean pointers are always read only ( using new also !!) ?

    thanks
    blue_gene

  4. #19
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    This is the C forum and not the C++ forum, soooo what you have is not valid.

    So new shoud be malloc and cout should be printf. Also s = "mango" will change s to pointer to the readonly location of "mango" (if it even compiles)

    Here is a proper example:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main (void)
    {
      char *s =  malloc(6);
      strcpy(s, "mango");
      puts(s);
      s[3] = 'P';
      puts(s);
      free(s);
      return 0;
    }
    Last edited by Thantos; 04-16-2004 at 09:41 PM.

  5. #20
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >what is unsigned ?? it should be unsigned int.
    unsigned and unsigned int are the same thing.

    >N.B : is it not necesarry to null terminate inside ?
    When you reverse a string you want the present nul to stay where it is, lest you end up with an empty string by swapping the nul character with the first character.
    My best code is written with the delete key.

  6. #21
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,607
    >> code-2 is also giving segmentaion fault !!

    Here's how you can declare s without having it point to dynamic or read-only memory:
    Code:
    #include <stdio.h>
    
    int main ()
    {
       char s[] = "mango";
       
       puts(s);
       s[3] = 'P';
       puts(s);
    
       return 0;
    }
    gg

Page 2 of 2 FirstFirst 12
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, 04:45 PM
  2. Reverse a string (without using any string functions?)
    By geetard in forum C Programming
    Replies: 2
    Last Post: 11-15-2006, 06:42 PM
  3. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 11:47 AM
  4. Replies: 7
    Last Post: 03-18-2003, 02:32 PM
  5. Overflowing string... any easy way to fix it?
    By Trauts in forum C++ Programming
    Replies: 2
    Last Post: 11-26-2002, 07:35 PM

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