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...

4
  • 1
    I don't think, that this works : char str2[strlen(string)]. Commented Nov 26, 2013 at 15:02
  • 3
    @Batuu C99 has Variable-Length Arrays Commented Nov 26, 2013 at 15:03
  • 1
    instead of creating str2, just work with the original string and, using pointers, swap characters. Commented Nov 26, 2013 at 15:03
  • @Deck Totally right. But Question is only tagged with general c Commented Nov 26, 2013 at 15:13

4 Answers 4

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;
          }
        }
}
Sign up to request clarification or add additional context in comments.

5 Comments

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 true that, it was a fun exercise for me on how fast I could chunk this out :)
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.
Ill fix the potential UB for completeness
Close enough. ding =P
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".

2 Comments

I want to modify the string from the function and I want the input string to be affected in main too.
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.
0

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

It will work just fine.

Comments

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);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.