C++ program to concatenate a string given number of times Last Updated : 16 Feb, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 Implementation: 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 temporary string. string s1 = s; for (int i=1; i<n;i++) s += s1; // Concatenating strings return s; } // Driver code int main() { string s = "geeks"; int n = 3; cout << repeat(s, n) << endl;; return 0; } 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 // temporary string. String s1 = s; for (int i = 1; i < n; i++) // Concatenating 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 Python3 # 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 # temporary string. s1 = s for i in range(1, n): # Concatenating strings s += s1 return s # Driver code s = "geeks" n = 3 print(repeat(s, n)) # This code is contributed # by Smitha 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 // temporary string. String s1 = s; for (int i = 1; i < n; i++) // Concatenating 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 JavaScript <script> // javascript program to concatenate given string // n number of times // Function which return string by concatenating it. function repeat(s, n) { // Copying given string to temporary string. let s1 = s; for (let i = 1; i < n; i++) s += s1; // Concatenating strings return s; } let s = "geeks"; let n = 3; document.write(repeat(s, n)); // This code is contributed by vaibhavrabadiya117 </script> Outputgeeksgeeksgeeks Time complexity: O(n) where n is number of times the string is repeated.Auxiliary Space: O(n) for storing temporary string. Comment More infoAdvertise with us Next Article C++ program to concatenate a string given number of times K kartik Improve Article Tags : Strings DSA Basic Coding Problems Practice Tags : Strings Similar Reads Reorder the given string to form a K-concatenated string Given a string S and an integer K. The task is to form a string T such that the string T is a reordering of the string S in a way that it is a K-Concatenated-String. A string is said to be a K-Concatenated-String if it contains exactly K copies of some string.For example, the string "geekgeek" is a 8 min read Maximum number of times a given string needs to be concatenated to form a substring of another string Given two strings S1 and S2 of length N and M respectively, the task is to find the maximum value of times the string S2 needs to be concatenated, such that it is a substring of the string S1. Examples: Input: S1 = "ababc", S2 = "ab"Output: 2Explanation: After concatenating S2 exactly twice, the str 12 min read Print Concatenation of Zig-Zag String in 'n' Rows Given a string and number of rows 'n'. Print the string formed by concatenating n rows when input string is written in row-wise Zig-Zag fashion. Examples: Input: str = "ABCDEFGH" n = 2 Output: "ACEGBDFH" Explanation: Let us write input string in Zig-Zag fashion in 2 rows. A C E G B D F H Now concate 12 min read Check if a string is concatenation of another given string Given two strings str1 and str2 of length N and M respectively, the task is to check if the string str1 can be formed by concatenating the string str2 repetitively or not. Examples: Input: str1 = âabcabcabcâ, str2 = âabcâOutput: YesExplanation: Concatenating the string str2 thrice generates the stri 7 min read Lexicographical concatenation of all substrings of a string Given a string, find the concatenation of all substrings in lexicographic order.Examples:Input : s = "abc"Output : aababcbbccThe substrings of s in lexicographic order are "a", "b", "c", "ab", "abc", "bc". Concatenation of substrings is "a"+"ab"+"abc"+"b"+"bc"+"c" = "aababcbbcc".Input : s = "cba"Out 7 min read Like