在C++程式設計中給字串拼接數字
一個在C++中給字串拼接數字的程式會根據n的值執行n次字串拼接方法。
結果將是該字串重複若干次。
示例
given string: “ I love Tutorials point” n = 5
輸出
I love Tutorials pointI love Tutorials pointI love Tutorials pointI love Tutorials point I love Tutorials point
看過輸出後,很明顯這個函式的作用是。
示例
#include <iostream>
#include <string>
using namespace std;
string repeat(string s, int n) {
string s1 = s;
for (int i=1; i<n;i++)
s += s1; // Concatinating strings
return s;
}
// Driver code
int main() {
string s = "I love tutorials point";
int n = 4;
string s1 = s;
for (int i=1; i<n;i++)
s += s1;
cout << s << endl;;
return 0;
}輸出
I love tutorials pointI love tutorials pointI love tutorials pointI love tutorials point
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP