Skip to content

Instantly share code, notes, and snippets.

@CraigRodrigues
Last active July 9, 2016 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CraigRodrigues/7d66a4c852a60bb3d323c4e4ab78da4a to your computer and use it in GitHub Desktop.
Save CraigRodrigues/7d66a4c852a60bb3d323c4e4ab78da4a to your computer and use it in GitHub Desktop.
Reverse String - Write a function that takes a string as input and returns the string reversed.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
char* reverseString(char* s) {
int length = 0, i = 0;
length = strlen(s);
char temp[1];
for(i = 0; i < length/2; i++)
{
*temp = s[i];
s[i] = s[length-i-1];
s[length-i-1] = *temp;
}
return s;
}
int main(void) {
char *str = malloc(sizeof(char *) * 250); //arbitrary memory allocation
scanf("%s", str); //get input from user
reverseString(str);
printf("%s\n", str);
free(str); //free memory
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment