---
title: 'UVa 483 題解 — C++'
disqus: hackmd
---
# UVa 483 題解 — C++
:::info
:bulb: 此筆記為UVa 483的題目詳解,包含解題思路、C++範例程式碼。
:::
## Word Scramble (OnlineJudge 483.)
### [題目](https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=0&problem=424)
:::success
Write a program that will reverse the letters in each of a sequence of words while preserving the order of the words themselves.
:::
### 輸入 / 輸出說明
| **輸入說明** | **輸出說明** |
|:-|:-|
| The input file will consist of several lines of several words. Words are contiguous stretches of printable characters delimited by white space. | The output will consist of the same lines and words as the input file. However, the letters within eachword must be reversed. |
### 解題思路
:::warning
這邊我不使用字串(string)而是字元陣列代替,基本上可以將字元陣列與字串看為同樣的東西,並且題目句子包含空格,因此要使用 cin.getline(str, 1000) 讀取整行,直到換行才會結束。
接著我透過 strtok 去分割字串,以空格為分界,然後將分割後的字串由後往前輸出,即可顛倒輸出。
:::
### 範例程式碼
```C++=
#include <bits/stdc++.h>
using namespace std;
int main()
{
char str[1000];
int len;
while (cin.getline(str, 1000)) {
char *token = strtok(str, " ");
while (token != NULL) {
len = strlen(token);
for (int i=len-1;i>=0;i--)
cout << token[i];
token = strtok(NULL, " ");
if (token != NULL)
cout << " ";
}
cout << endl;
}
return 0;
}
```
### 運行結果
<font color="#00BB00">**AC**</font> 0.000s
###### tags: `CPE ?星`
:::danger
查看更多資訊請至:https://www.tseng-school.com/
:::