1. 程式人生 > >[leetcode]Two Pointers-344. Reverse String

[leetcode]Two Pointers-344. Reverse String

malloc pre sed class col ever func -- take

Write a function that takes a string as input and returns the string reversed.

Example:
Given s = "hello", return "olleh".

char* reverseString(char* s) {  
2     assert(s != NULL);  
3     int len= strlen(s)-1;  
4     char* tmp = (char *)malloc(len+2);  
5     for(int i = len; i>=0; --i)  
6         tmp[len-i] = s[i];  
7 tmp[len+1]=\0; 8 return tmp; 9 }

[leetcode]Two Pointers-344. Reverse String