首頁 >後端開發 >C++ >根據單字數量反轉字串

根據單字數量反轉字串

WBOY
WBOY轉載
2023-09-03 15:09:06952瀏覽

根據單字數量反轉字串

String manipulation is an essential skill in programming, as it helps us process and analyze text data efficiently. C provides a rich set of string manipulation functions and objects, string manipulation functions and objects text data.

In this article, we will discuss how to reverse a string according to the number of words in C .

方法

Approach 1 − Using stringstreams and vectors

方法2 - 使用子字串和字串運算子

Syntax

在C 中的字串物件:std::string類別是C 標準函式庫的一部分,提供了各種字串操作函數。

String manipulation functions: Some common string manipulation functions in C include length(), substr(), find(), erase(), and replace().

std::string reverseStringByWords(const std::string& input) {}
std::reverse(words.begin(), words.end());

Approach 1:- Using stringstreams and vectors

This approach employed in the code involves converting the input string into a sequence of words using a stringstream object. The words are then extracted one by one from the stream and stored in a collection of strings.

Subsequently, the collection of words is reversed using the reverse function from the algorithm library. The reversed words are then joined together to form the final output string, with a space appended after together form the final output string, with a space appended after

演算法

    開始
  • 取得輸入字串。
  • 從輸入字串建立一個stringstream。
  • 初始化一個空向量來儲存單字。
  • 透過stringstream迭代提取單字。
  • Extract a word from the stringstream.
  • #Push the extracted word into the vector.
  • Reverse the vector containing the words.
  • #初始化一個空的輸出字串。
  • 透過反向遍歷向量形成輸出字串。
  • Add each word from the reversed vector to the output string, followed by a space.
  • Remove the last space from the output string.
  • 傳回輸出字串。
  • End
  • Example

The code embodies a procedure that inverts the sequence of words in a specified string. This is accomplished by first transforming the input string into a word-based stream through utilization of a stringstream object.psered stract the Subword exed exse str. time and placed into a vector. Then, the reverse function from the algorithm library is employed to reverse the v? The last space is then deleted from the output string, rendering a compact and comprehensible solution that makes the most of Standard Library features like stringstreams, vectors, and the algorithm library.

  • #'
    #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;
    }
    

    Output

    的中文翻譯為:
  • 輸出
  • Input: Hello, how are you?
    Output: you? are how Hello,
    

    方法2:使用子字串和字串運算子

    ###方法2是將字串中的單字順序顛倒的另一個解決方案。它使用子字串和字串操作函數,而不是像方法1那樣使用字串流和向量。 ### ###This approach involves manually dividing the input string into substrings, which represent individual words. The substrings are concatenated in reverse order to form the final output string.### ###演算法### ### ######開始###### ######取得輸入字串。 ###### ######初始化兩個 size_t 變數 start 和 end,用於保存輸入字串中單字的起始和結束位置。 ###### ######將起始位置初始化為0。 ###### ######找到輸入字串中第一個空格的位置,並將其儲存在end變數中。 ###### ######初始化一個空的輸出字串。 ###### ######Iterate through the input string and extract words using substrings.###### ######從起始位置提取子字串到結束位置。 ###### ######Concatenate the extracted substring in front of the output string, followed by a space.###### ######Update the start position to the position after the end position.###### ######Find the next space in the input string starting from the new start position and update the end position.####### ######循環結束後,使用####### ######End####

Example

The code is a solution for reversing the order of words in a given string. It does this by dividing the input string into substrings using the find function and concatenating these substrings in reverse order to form the output string. The last space character is then removed from the output string using the pop_back function. This approach is more manual and low-level compared to Approach 1 and requires a deeper understanding of string manipulation. The code takes a given input string, divides it into substrings, reverses the order of these substrings, and returns the final output string.

#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;
}

Output

的中文翻译为:

输出

Input: Hello, how are you?
Output: you? are how Hello,

结论

在编程中字符串操作的重要性:掌握字符串操作技术对于任何程序员来说都是至关重要的,因为文本数据在软件开发中无处不在。理解各种字符串操作的方法可以帮助开发人员编写更高效、可维护和健壮的代码。

以上是根據單字數量反轉字串的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除

相關文章

看更多