Created
July 22, 2021 14:14
-
-
Save ruanpetterson/8c00dafedf9426f1f12361643e378b95 to your computer and use it in GitHub Desktop.
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 *a, char* b); | |
void reverse(char* s); | |
int main() { | |
char s[] = "Hello, world!"; | |
printf("Before: %s\n", s); | |
reverse(s); | |
printf("After: %s\n", s); | |
return 0; | |
} | |
void swap(char* a, char* b) { | |
char c = *a; | |
*a = *b; | |
*b = c; | |
} | |
void reverse(char* s) { | |
if (!*s) | |
return; | |
char* head = s; | |
char* tail = head + strlen(s) - 1; | |
while (head <= tail) { | |
swap(head, tail); | |
head++; | |
tail--; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment