如何在 C++ 中將多個字串連線在一行?


在 C++ 中,我們將學習如何在一行中連線多個字串。有幾種不同的方法可以做到這一點。最簡單的方法是使用加號 (+) 運算子。字串可以透過 + 連線。我們可以在兩個字串之間放置 + 號來使它們連線。

Input: Some strings “str1”, “str2”, “str3”
Output: Concatenated string “str1str2str3”

演算法

Step 1: Take some strings
Step 2: Concatenate them by placing + sign between them
Step 3: Display the string
Step 4: End

示例程式碼

 即時演示

#include <iostream>
using namespace std;
int main() {
   string str1, str2, str3;
   str1 = "Hello";
   str2 = "C++";
   str3 = "World";
   string res = str1 + str2 + str3;
   cout << res;
}

輸出

HelloC++World

更新於: 2019-07-30

889 次瀏覽

開啟您的 職業生涯

完成課程即可獲得認證

開始
廣告
© . All rights reserved.