Skip to content

Instantly share code, notes, and snippets.

@CraigRodrigues CraigRodrigues/reverse.c
Last active Jul 9, 2016

Embed
What would you like to do?
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
You can’t perform that action at this time.