在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