带有指定重复次数的 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