Skip to content

Instantly share code, notes, and snippets.

@alexgeek
Last active August 29, 2015 14:16
Show Gist options
  • Save alexgeek/f6aa358a4791be30b56c to your computer and use it in GitHub Desktop.
Save alexgeek/f6aa358a4791be30b56c to your computer and use it in GitHub Desktop.
Reversing a String
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
inline void swap(char * str, int a, int b) {
char * tmp = str[a];
str[a] = str[b];
str[b] = tmp;
}
#define INLINE_SWAP(str, a, b) {char * tmp = str[a]; str[a] = str[b]; str[b] = tmp;}
int main (int argc, char * const argv[])
{
char * str = strdup("Hello World");
const int len = strlen(str);
int i;
for(i = 0; i < len/2; i++)
swap(str, i, len-(i+1));
printf("%s\n", str);
free(str);
return 0;
}
@alexgeek
Copy link
Author

alexgeek commented Mar 6, 2015

About

A bit of C that reverses a string. It uses strdup so that we can modify the string.
I tried using a macro but there was no discernible difference in execution time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment