Skip to content
Related Articles

Related Articles

C++ program to concatenate a string given number of times

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 12 Jul, 2022
Improve Article
Save Article

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>

Output

geeksgeeksgeeks

Time complexity: O(n) where n is number of times the string is repeated.
Auxiliary Space: O(n) for storing temporary string.

This article is contributed by Sahil Rajput. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. 


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!