Reversing string

This is a discussion on Reversing string within the C++ Programming forums, part of the General Programming Boards category; hi, please see the code below: void main() { int i,len; char* str = "shalaka"; char temp; len=strlen(str) - 1; ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    41

    Reversing string

    hi,

    please see the code below:

    void main()
    {

    int i,len;
    char* str = "shalaka";
    char temp;

    len=strlen(str) - 1;
    for(i=0;i<strlen(str)/2;i++)
    {
    temp = str[i];
    str[i] = str[len];
    str[len--]=temp;

    }

    }

    str[i] = str[len]; => my program throws an exception at this line . Why?


    Best Regards,
    Shalak

  2. #2
    すまん Hikaru's Avatar
    Join Date
    Aug 2006
    Posts
    46
    I don't think you can change a string literal. Try making str an array instead of a pointer.

  3. #3
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    use code tags.

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,435
    Code:
    int main()
    and

    Code:
    char str[] = "shalaka";
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    41
    Ya, making "str" as an array works! But y does it not work with pointers?

  6. #6
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    *shrug* no idea, can't read code when there isn't code tags around it, perhaps edit the post and put in the code tags and repaste the code so there is formating.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    The pointer points to a string literal, which is a constant that cannot be modified by the program (which is why you should be using const char* if you use a pointer). Using the array actually creates an array in memory and copies the contents of that string literal into the array. In that case it is yours to change however you'd like.

    BTW, if you are using C++, you should probably be learning the C++ string class first instead of C style string like those that you are using.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  2. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. problems with overloaded '+' again
    By Brain Cell in forum C++ Programming
    Replies: 9
    Last Post: 04-14-2005, 05:13 PM
  5. Reversing a String
    By ToasterPas in forum C++ Programming
    Replies: 10
    Last Post: 08-14-2001, 12:20 PM

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