Last active
July 9, 2016 14:22
-
-
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.
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> | |
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