0

I am trying to reverse a string with a function and some pointers, but I can't get the function to update the original string.

#include <stdio.h>
#include <string.h>


void rev(char* string)
{
    char str2[strlen(string)];
    char *p1;
    char *p2;

    p1 = string + strlen(string)-1;
    p2 = str2;

    while(p1 >= string)
        *p2++ = *p1--;

    *p2 = '\0';
    p2 = p2 - strlen(string);

    string = p2; // This codesn't seem to update s1 or s2

}

int main(void)
{
    char s1[100] = "What does the fox say?";
    char s2[100] = "Titanic sinks";

    rev(s1);
    rev(s2);

    printf("\n\n%s\n", s1); 
    printf("%s\n", s2);

    return 0;
}

The functionality works but I can't get the strings in main to get updated with the reversed string. Imo string = p2 should update the string to the reversed value of it. It does, but only within the function, not in the main function...

user3032809
  • 33
  • 1
  • 2
  • 6

4 Answers4

5

You also need to implement swap semantics otherwise you are losing original characters

    void rev(char *s)
    {
        if (s != NULL)
        {
          int n = strlen(s) - 1;
          char *p1 = s;
          char *p2 = s + max(n, 0);

          while (p2 > p1)
          {
              char temp = *p1;
              *p1++ = *p2;
              *p2-- = temp;
          }
        }
}
Serve Laurijssen
  • 9,266
  • 5
  • 45
  • 98
  • 1
    Apart from the undefined behavior of this when passed an empty string, it is far closer than any other answer posted. And even that UB can be addressed with proper repositioning of the post-decement and removal of the `-1` from the `p2` initializer. – WhozCraig Nov 26 '13 at 15:05
  • @WhozCraig true that, it was a fun exercise for me on how fast I could chunk this out :) – Serve Laurijssen Nov 27 '13 at 07:16
  • If you fix the UB (which isn't entirely obvious) in the `(p2 > p1)` comparison i'll even up-vote it for you. `p2` should be `s + strlen(s);` the comparison should be `while (p1 < p2--)`, and the assignment dereference of `p2` should be stripped of the decrement operator; `*p2 = temp;`. Think about it for awhile and then remember, value comparisons of array sequences per the standard are valid from the base to one-*past* the end *only*. It is not technically valid to compare against one-*before* the base, which is what will happen if you pass an *empty* string to the current code. – WhozCraig Nov 27 '13 at 07:52
  • Ill fix the potential UB for completeness – Serve Laurijssen Nov 27 '13 at 08:03
  • Close enough. ding =P – WhozCraig Nov 27 '13 at 08:09
0

C uses pass by value semantics with pointers as well as for any other values. When you pass a pointer to a function, its value gets copied to the variable string. Any alterations to string will remain local to the function (but alterations to the data pointed to will remain). You might want to return a pointer to the modified data instead or modify the string "in-place".

kviiri
  • 3,282
  • 1
  • 21
  • 30
  • I want to modify the string from the function and I want the input string to be affected in main too. – user3032809 Nov 26 '13 at 15:02
  • Then you need to modify the string in-place, or create a new string that represents the modified string and return a pointer to it. Simply changing the pointer is not enough because only a copy of the pointer is modified. – kviiri Nov 26 '13 at 15:05
0

Well, you can change this: string = p2; to this: strcpy(string, p2);.

It will work just fine.

ddz
  • 526
  • 3
  • 15
0

You can also use strrev function. In string.h, it is defined as below:

char * strrev (char *)

It changes all the characters in a string to reverse order except the terminating null character.

For example the code below change the array to reverse order and prints it on the screen:

char *a = "HELLO";

strrev(a);

printf("%s", a);

Melika Barzegaran
  • 429
  • 2
  • 9
  • 25