标签:
Write a function that takes a string as input and returns the string reversed.
Example:
Given s = "hello", return "olleh".
将一个给定的字符串倒过来输出。
Subscribe to see which companies asked this question
1 char* reverseString(char* s) { 2 3 int left,right; 4 char *tmp; 5 left = 0; 6 right = strlen(s)-1; 7 8 //两边同时移动指针赋值 9 while(left < right ){ 10 tmp = s[left]; 11 s[left++] = s[right]; 12 s[right--] = tmp; 13 } 14 15 return s; 16 }
上面的方法未多分配多余的内存空间,输出和输入的内存一样。
标签:
原文地址:http://www.cnblogs.com/nm90/p/5699626.html