Created
January 5, 2014 21:00
-
-
Save rodchile/8273799 to your computer and use it in GitHub Desktop.
Implementing a C string reverser
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 <stdlib.h> | |
#include <stdio.h> | |
#include <string.h> | |
void reverseString (char **str) | |
{ | |
char *strCopy = *str; | |
int size = strlen(strCopy) - 1; | |
char *aux = strdup(strCopy); | |
for (int i = size ; i >= 0; --i) | |
{ | |
aux[size - i] = strCopy[i]; | |
} | |
*str = aux; | |
printf("String value reversed: %s\n", aux); | |
} | |
int main(int argc, char const *argv[]) | |
{ | |
char *strToReverse = "hola"; | |
printf("Value previous to reverse %s\n", strToReverse); | |
reverseString(&strToReverse); | |
printf("Value after the reverse %s\n", strToReverse); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment