[leetcode]Two Pointers-344. Reverse String

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 }  

 

posted @ 2018-01-13 18:36  chenhan233  阅读(92)  评论(0)    收藏  举报