> For the complete documentation index, see [llms.txt](https://stanley7342.gitbook.io/programming/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://stanley7342.gitbook.io/programming/leetcode/344.-reverse-string.md).

# 344. Reverse String

## 題目原文

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

#### Example 1:

```
Input: "hello"
Output: "olleh"
```

#### &#x20;Example 2:

```
Input: "A man, a plan, a canal: Panama"
Output: "amanaP :lanac a ,nalp a ,nam A"
```

## **解題思路**

{% hint style="info" %}

1. 第一個字元與最後一個字元交換。
2. 第二個字元與倒數第二個字元交換。
3. 以此類推。
   {% endhint %}

## 程式解答

```cpp
class Solution 
{
public:
    string reverseString(string s) 
    {
        int size = s.length();
    
        for (int i = 0; i < size / 2; i++)
        {
            char temp;
            
            temp = s[i];
            s[i] = s[size - 1 - i];
            s[size - 1 - i] = temp; 
        }
        return s;
    }
};
```
