Skip to content

Instantly share code, notes, and snippets.

@ruanpetterson
Created July 22, 2021 14:14
Show Gist options
  • Save ruanpetterson/8c00dafedf9426f1f12361643e378b95 to your computer and use it in GitHub Desktop.
Save ruanpetterson/8c00dafedf9426f1f12361643e378b95 to your computer and use it in GitHub Desktop.
#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