Created
January 18, 2018 12:49
-
-
Save kira924age/978367ee75c7d6fbfc0aa7f798b4630b to your computer and use it in GitHub Desktop.
Reverse string.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
void swap(char *x, char *y) { | |
char tmp; tmp = *x; | |
*x = *y; | |
*y = tmp; | |
} | |
void *reverse(char *s) { | |
char *p = s, *q = s + strlen(s) - 1; | |
int t = p - q; | |
while ((p - q) * t >= 0) { | |
swap(p, q); | |
*p++; *q--; | |
} | |
return s; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment