344. Reverse String

 
 My Submissions
 
  • Total Accepted: 57497
  • Total Submissions: 97908
  • Difficulty: Easy

 

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 }
View Code

 上面的方法未多分配多余的内存空间,输出和输入的内存一样。

posted on 2016-07-23 22:05  人生一世,草木一秋。  阅读(391)  评论(0编辑  收藏  举报