Reverse String

题目要求:

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

Example:

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

题目大意:

输入一个字符串,将其按倒序输出。

例子:输入s='hello', 则输出'olleh'。

解题思路

利用string字符串数组找到中间字符,将前后字符依次交换位置,遍历输出。

 

C++代码:

 1 class Solution {
 2 public:
 3    string reverseString(string s) {
 4        int i, n;
 5        char temp;
 6        n=s.size();
 7 
 8        for(i=0; i<n/2; i++)
 9        {
10            temp=s[i];
11            s[i] = s[n-i-1];
12            str[n-i-1] = temp;
13        }
14 
15        return s;
16    }
17 };

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-10-06
  • 2021-09-03
  • 2021-07-04
  • 2021-12-26
  • 2021-12-12
猜你喜欢
  • 2021-04-24
  • 2022-02-12
  • 2022-12-23
  • 2021-10-22
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案