0

I am writing a code to reverse a string in place:

void ReverseInPlace(char * x)
{
    char * end = x;
    int i;
    i = 0;
    char temp;
    while(*end)
    {
        ++end;//point tol end 
        ++i;//count the lenght
    }
    --end;
    printf("%s",end);

    printf("%d",i);
    while(i)
    {
        temp = *end;
        printf("%s","\n");
        printf("%c",temp);

        end--;
        x++;
        i--;
    }
    printf("%s","\n");//blank line
    printf("%s","\n");//blank line
    printf("%s","-----");//blank line
    printf("%s",x);//print original 
}

Here are my confusions:
Even though I am able to print the characters in reverse, I want to reverse the string without using an array

I get error when I try to do the following:

*x = temp;
  • 6
    How are you calling the function? If it's like `ReverseInPlace("hi")` then it won't work because you are modifying a string in non-writable memory – Seth Carnegie Jan 15 '12 at 17:00
  • in your code `x` is a `char *` and `temp` is a `char`, so the expression statement `*x = temp;` is fine and you should not get an error. What error you get? – ouah Jan 15 '12 at 17:01
  • @danihp he's saying it crashes when he adds `*x = temp;` – Seth Carnegie Jan 15 '12 at 17:08
  • yes I am calling the function like ReverseInPlace("123456789"); –  Jan 15 '12 at 17:10
  • Yes I get crash when I do *x = temp –  Jan 15 '12 at 17:10
  • You can't modify a string literal. That's what Seth was trying to tell you. The compiler allows you to treat literals as if they were not declared `const`, but you should never do this because attempting to modify them produces undefined behavior. The way you're calling the function is incorrect and bound to fail. – Cody Gray - on strike Jan 15 '12 at 17:12
  • Possible duplicate of http://stackoverflow.com/questions/198199/how-do-you-reverse-a-string-in-place-in-c-or-c –  Jan 15 '12 at 17:39

3 Answers3

2

You say you are doing this:

ReverseInPlace("123456789");

You need to do something like this:

char str[] = "123456789";
ReverseInPlace(str);

Doing it the latter way allocates storage which you can modify, as opposed to modifying a literal string, which is illegal.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
0

char *p="abcd123"; // ........string literal stored in read only memory mostly in text segment(where the code is stored) so you should never change value here as it may crash your program by writing onto code. Here p is pointing to address which maybe in text segment.

char q[]="abcd1234"; //..... the values can be changed and array size can be modified but address cant be changed because the array is nothing but a constant pointer. That's the problem with your code .....you are calling the function and parameter is a string literal whereas it should be a constant pointer. Further the storage class here is stack so u can modify the values.

hanish
  • 67
  • 6
0

My proposal

#include <stdio.h> // printf
#include <string.h> // strlen

void swap(char* a , char* b)
{
    char tmp;
    tmp=*a;
    (*a) = (*b);
    (*b) = tmp;
}

void ReverseInPlace(char * x)
{
    char * end = x;
    int i,j,length;
    char temp;

    length = strlen(x);

    //swap 1st with last, then 2nd with last-1, etc.  Till we reach the middle of the string.
    for(i=0,j=length-1 ; i<j ; ++i,--j)
        swap( &x[i] , &x[j]);
}

main (){
    char str[] = "123456789";
    ReverseInPlace(str);
    //ReverseInPlace("1234"); // this would lead to error, because "1234", can not be modified
    printf("%s\n",str);
}

After middle is reached, you would swap elements which were already swapped by previous iterations. For illustration:

char* x = "1234";
1 2 3 4
4 2 3 1
4 3 2 1 // already reversed!
4 2 3 1
1 2 3 4 // if we continue till i==length-1 && j=0 , then we just get where we started
dnsmkl
  • 792
  • 5
  • 17
  • I have a question :*a = *b : how come it does not crash ? –  Jan 15 '12 at 17:52
  • a - pointer to char - address of some place in memory. Imagine it to be something like 756483. *a - refers to actual value stored at that address. Same thing with b and *b. So in the end *a=*b, just takes value from memory segment that has address b and puts that value into memory segment that has address a. Or I have misunderstood your question? – dnsmkl Jan 15 '12 at 17:59
  • @RahulRai It actually will crash if you pass ReverseInPlace a literal string. Note the commented out line in the main function. If *a isn't writable memory then as soon as the program tries to store the contents of *b in *a it crashes. – wollw Jan 15 '12 at 18:04