ที่นี่เราจะมาดูวิธีการเชื่อมหลายสตริงในหนึ่งบรรทัดใน 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