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
热门推荐
1 带狗狗训练
4 听力训练狗狗
6 怎训练狗狗
7 腹肌训练狗狗
8 网球训练狗狗
9 记叙文训练狗狗
10 狗狗太凶怎么训练
11 核心力量训练狗狗
12 狗狗乱叫怎么训练
13 发音训练狗狗
14 怎么训练狗狗叼东西给主人
15 荆州训练狗狗
16 篮球训练狗狗
17 做训练狗狗
18 训练狗狗坐姿