- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ program to concatenate a string given number of times?
Here we will see how we can concatenate a string n number of times. The value of n is given by the user. This problem is very simple. In C++ we can use + operator for concatenation. Please go through the code to get the idea.
Algorithm
concatStrNTimes(str, n)
begin res := empty string for i in range 1 to n, do res := concatenate res and res done return res end
Example
#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; }
Output
Enter String: Hello Enter number of times it will be concatenated: 5 Result: HelloHelloHelloHelloHello
Advertisements