manipulating string pointer

This is a discussion on manipulating string pointer within the C Programming forums, part of the General Programming Boards category; Hi, I've got character pointer, pointing to a string. I want to reverse character without using any other variable. The ...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    2

    manipulating string pointer

    Hi,

    I've got character pointer, pointing to a string. I want to reverse character without using any other variable. The function is as fallow:

    void reverseString(char* str)
    {
    int len = strlen(str);
    int index = 0;
    int count = len/2;
    int end = len-1;
    for(int i=0;i<count;i++)
    {
    char temp = *(str+index);
    *(str+index) = *(str+end); //Crashes here
    *(str+end) = temp;
    index++;
    end--;
    }
    }

    But the program crashes at line *(str+index) = *(str+end)
    What 's the issue with that?

    Thanks,
    Khurram.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    First problem is you ARE using a separate extra variable - temp. Second, you should declare variables outside loops, instead of having them re-declared inside them.

    A few print or watch statements in your code, would really point to any problems. If you're going to program, you MUST learn to debug, and this is a gentle introduction as any.

    Please post code that is surrounded by code tags - highlight it, and click on the # icon at the top of the advanced reply editor window. That will keep your code from getting all squashed over to the far left hand side.

    And welcome to the forum, dude!


  3. #3
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Quote Originally Posted by kmirza View Post
    But the program crashes at line *(str+index) = *(str+end)
    What 's the issue with that?
    Your code ran ok on my machine - how are you calling the function (i.e., what are you passing as your string), and also, what is the crash message?

    BTW, here is your code, with some indentation (and a couple of minor mods).

    Code:
    void reverseString(char *str)
    {
        size_t len = strlen(str);
        int index = 0;
        int count = len / 2;
        int end = len - 1;
        int i;
        for (i = 0; i < count; i++) {
            char temp = *(str + index);
            *(str + index) = *(str + end);  //Crashes here
            *(str + end) = temp;
            index++;
            end--;
        }
    }
    Last edited by kermit; 07-18-2010 at 09:31 PM.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
        size_t len = strlen(str);
        int index = 0;
        int count = len / 2;
        int end = len - 1;
        int i;
        for (i = 0; i < count; i++) {
            char temp = *(str + index);
            *(str + index) = *(str + end);  //Crashes here
            *(str + end) = temp;
            index++;
            end--;
        }
    
    
      int i, len;
      char *begin, *end, *temp;
      
      begin = str;
      end = str + strlen(str) - 1;
      //printf("\n Begin=%c, End=%c", *begin, *end); //debug help
    
      while(begin < end) {
        *temp = *begin;
        *begin = *end;
        *end = *temp;
        begin++;
        end--;
        
      }

  5. #5
    Registered User
    Join Date
    Jul 2010
    Posts
    2
    The Problem is resolved. The issue was, I was passing string literal as character pointer which is constant. That's why it was throwing exception at runtime while changing that string. Now i'm passing array of character to function instead of character pointer.

    Thanks,

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 01:56 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 10:37 PM

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