帶有指定重複次數的 C++ 連線字串程式
此處我們將瞭解如何將一個字串連線 n 次。n 的值由使用者給出。此問題非常簡單。在 C++ 中,我們可以使用 + 運算子進行連線。請完整閱讀程式碼以瞭解其思想。
演算法
concatStrNTimes(str, n)
begin res := empty string for i in range 1 to n, do res := concatenate res and res done return res end
示例
#include<iostream>
using namespace std;
main() {
string myStr, res = "";
int n;
cout << "Enter String: ";
cin >> myStr;
cout << "Enter number of times it will be concatenated: ";
cin >> n;
for(int i= 0; i < n; i++) {
res += myStr;
}
cout << "Result: " << res;
}輸出
Enter String: Hello Enter number of times it will be concatenated: 5 Result: HelloHelloHelloHelloHello
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP