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