Skip to content

Instantly share code, notes, and snippets.

@rodchile
Created January 5, 2014 21:00
Show Gist options
  • Save rodchile/8273799 to your computer and use it in GitHub Desktop.
Save rodchile/8273799 to your computer and use it in GitHub Desktop.
Implementing a C string reverser
#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