C++ program to concatenate a string given number of times
Write a function which returns a new string which is made by concatenating a given string n number of times.
Examples:
Input : str = "geeks"
n = 3
Output : str = "geeksgeeksgeeks"
We concatenate "geeks" 3 times
Input : str = "for"
n = 2
Output : str = "forfor"
We concatenate "for" 2 times
CPP
// C++ program to concatenate given string // n number of times #include <bits/stdc++.h> #include <string> using namespace std; // Function which return string by concatenating it. string repeat(string s, int n) { // Copying given string to temparory string. string s1 = s; for (int i=1; i<n;i++) s += s1; // Concatinating strings return s; } // Driver code int main() { string s = "geeks"; int n = 3; cout << repeat(s, n) << endl;; return 0; } |
chevron_right
filter_none
Java
// Java program to concatenate given // string n number of times class GFG { // Function which return string by // concatenating it. static String repeat(String s, int n) { // Copying given string to // temparory string. String s1 = s; for (int i = 1; i < n; i++) // Concatinating strings s += s1; return s; } // Driver code public static void main(String[] args) { String s = "geeks"; int n = 3; System.out.println(repeat(s, n)); } } // This code is contributed by Smitha |
chevron_right
filter_none
Pyhton 3
# Python 3 program to concatenate # given string n number of times # Function which return string by # concatenating it. def repeat(s, n): # Copying given string to # temparory string. s1 = s for i in range(1, n): # Concatinating strings s += s1 return s # Driver code s = "geeks"n = 3print(repeat(s, n)) # This code is contributed # by Smitha |
chevron_right
filter_none
C#
// C# program to concatenate given // string n number of times using System; class GFG { // Function which return string // by concatenating it. static String repeat(String s, int n) { // Copying given string to // temparory string. String s1 = s; for (int i = 1; i < n; i++) // Concatinating strings s += s1; return s; } // Driver code public static void Main() { String s = "geeks"; int n = 3; Console.Write(repeat(s, n)); } } // This code is contributed by Smitha |
chevron_right
filter_none
Output:
geeksgeeksgeeks
This article is contributed by Sahil Rajput. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Concatenate suffixes of a String
- Number of subsequences as "ab" in a string repeated K times
- Methods to concatenate string in C/C++ with Examples
- Repeat substrings of the given String required number of times
- Number of times the given string occurs in the array in the range [l, r]
- Concatenate strings in any order to get Maximum Number of "AB"
- Longest sub string of 0's in a binary string which is repeated K times
- String which when repeated exactly K times gives a permutation of S
- Largest sub-string where all the characters appear at least K times
- Print the string after the specified character has occurred given no. of times
- Encrypt a string by repeating i-th character i times
- Python program to count number of vowels using sets in given string
- Decrypt a string encrypted by repeating i-th character i times
- Check if max occurring character of one string appears same no. of times in other
- Remove characters from string that appears strictly less than K times
Improved By : Smitha Dinesh Semwal



