句子单词翻转 - C++ 实现
{"title":"句子单词翻转 - C++ 实现","description":"本程序使用 C++ 实现将输入句子中每个单词进行翻转的功能,并保持原句的空格。","keywords":"句子翻转, 单词翻转, C++, 字符串处理, 算法","content":"#include "iostream"\n#include "string"\n#include "sstream"\n\nusing namespace std;\n\nstring reverseWord(string word) {\n int n = word.length();\n for (int i = 0; i < n / 2; i++) {\n swap(word[i], word[n - i - 1]);\n }\n return word;\n}\n\nint main() {\n string sentence;\n getline(cin, sentence);\n\n stringstream ss(sentence);\n string word;\n bool firstWord = true;\n while (ss >> word) {\n if (!firstWord) {\n cout << " " ;\n }\n cout << reverseWord(word);\n firstWord = false;\n }\n\n return 0;\n}
原文地址: https://www.cveoy.top/t/topic/p66u 著作权归作者所有。请勿转载和采集!