根據單詞數量反轉字串
字串操作是程式設計中一項必不可少的技能,因為它可以幫助我們高效地處理和分析文字資料。C++ 提供了一套豐富的字串操作函式和物件,使處理文字資料變得更加容易。
在本文中,我們將討論如何在 C++ 中根據單詞數量反轉字串。
方法
方法 1 − 使用字串流和向量
方法 2 − 使用子字串和字串操作函式
語法
C++ 中的字串物件:std::string 類是 C++ 標準庫的一部分,並提供了各種字串操作函式。
字串操作函式:C++ 中一些常見的字串操作函式包括 length()、substr()、find()、erase() 和 replace()。
std::string reverseStringByWords(const std::string& input) {} std::reverse(words.begin(), words.end());
方法 1:- 使用字串流和向量
程式碼中採用的這種方法涉及使用字串流物件將輸入字串轉換為一系列單詞。然後,從流中逐個提取單詞,並存儲在由向量表示的字串集合中。
隨後,使用演算法庫中的 reverse 函式反轉單詞集合。然後將反轉後的單詞連線在一起形成最終的輸出字串,除了最後一個單詞之外,每個單詞後面都附加一個空格。
演算法
開始
獲取輸入字串。
從輸入字串建立字串流。
初始化一個空向量來儲存單詞。
遍歷字串流以提取單詞。
從字串流中提取一個單詞。
將提取的單詞推入向量中。
反轉包含單詞的向量。
初始化一個空輸出字串。
遍歷反轉後的向量以形成輸出字串。
將反轉後的向量中的每個單詞新增到輸出字串中,後跟一個空格。
從輸出字串中刪除最後一個空格。
返回輸出字串。
結束
示例
該程式碼體現了一個反轉指定字串中單詞順序的過程。這是透過首先利用字串流物件將輸入字串轉換為基於單詞的流來實現的。隨後,單詞被逐個提取並放入向量中。然後,使用演算法庫中的 reverse 函式反轉向量。最後,反轉後的單詞連線在一起形成最終的輸出字串,並在每個單詞(最後一個單詞除外)後插入空格。然後從輸出字串中刪除最後一個空格,從而提供了一個簡潔易懂的解決方案,該方案充分利用了標準庫的功能,如字串流、向量和演算法庫。
#include <iostream> #include <string> #include <sstream> #include <vector> #include <algorithm> std::string reverseStringByWords(const std::string& input) { std::stringstream ss(input); std::string word; std::vector<std::string> words; while (ss >> word) { words.push_back(word); } std::reverse(words.begin(), words.end()); std::string output; for (const auto& w : words) { output += w + " "; } output.pop_back(); // Remove the last space return output; } int main() { std::string input = "Hello, how are you?"; std::string output = reverseStringByWords(input); std::cout << "Input: " << input << std :: endl; std:: cout << "Output: " << output << std :: endl; return 0; }
輸出
Input: Hello, how are you? Output: you? are how Hello,
方法 2:- 使用子字串和字串操作函式
方法 2 是反轉字串中單詞順序的另一種解決方案。它使用子字串和字串操作函式,而不是像方法 1 中那樣使用字串流和向量。
此方法涉及手動將輸入字串劃分為子字串,這些子字串表示各個單詞。子字串以相反的順序連線起來形成最終的輸出字串。
演算法
開始
獲取輸入字串。
初始化兩個 size_t 變數 start 和 end,以儲存輸入字串中單詞的起始和結束位置。
將起始位置初始化為 0。
查詢輸入字串中第一個空格的位置,並將其儲存在 end 變數中。
初始化一個空輸出字串。
遍歷輸入字串並使用子字串提取單詞。
從起始位置到結束位置提取子字串。
將提取的子字串連線到輸出字串的前面,後跟一個空格。
將起始位置更新到結束位置之後的位置。
從新的起始位置開始查詢輸入字串中的下一個空格,並更新結束位置。
迴圈結束後,使用
結束
示例
該程式碼是用於反轉給定字串中單詞順序的解決方案。它透過使用 find 函式將輸入字串劃分為子字串,並將這些子字串以相反的順序連線起來以形成輸出字串來實現此目的。然後,使用 pop_back 函式從輸出字串中刪除最後一個空格字元。與方法 1 相比,此方法更手動且更低階,需要更深入地瞭解字串操作。該程式碼獲取給定的輸入字串,將其劃分為子字串,反轉這些子字串的順序,並返回最終的輸出字串。
#include <iostream> #include <string> std::string reverseStringByWords(const std::string& input) { size_t start = 0; size_t end = input.find(' '); std::string output; while (end != std::string::npos) { output = input.substr(start, end - start) + " " + output; start = end + 1; end = input.find(' ', start); } output = input.substr(start) + " " + output; output.pop_back(); // Remove the last space return output; } int main() { std::string input = "Hello, how are you?"; std::string output = reverseStringByWords(input); std::cout << "Input: " << input << std::endl; std::cout << "Output: " << output << std::endl; return 0; }
輸出
Input: Hello, how are you? Output: you? are how Hello,
結論
字串操作在程式設計中的重要性:掌握字串操作技巧對於任何程式設計師來說都是至關重要的,因為文字資料在軟體開發中無處不在。瞭解各種字串操作方法可以幫助開發人員編寫更高效、更易維護和更健壯的程式碼。