Last active
August 29, 2015 14:16
-
-
Save alexgeek/f6aa358a4791be30b56c to your computer and use it in GitHub Desktop.
Reversing a String
This file contains hidden or 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 <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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.