在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

更新於: 09-Aug-2019

680次瀏覽

開啟你的事業

透過完成課程獲得認證

開始
廣告
© . All rights reserved.