The Wayback Machine - https://web.archive.org/web/20240717024425/https://www.geeksforgeeks.org/create-a-string-of-specific-length-in-cpp/
Open In App

Create a string of specific length in C++

Last Updated : 16 Jul, 2021
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

C++ has in its definition a way to represent a sequence of characters as an object of a class. This class is called std::string. String class stores the characters as a sequence of bytes with the functionality of allowing access to the single-byte characters. This article focuses on discussing how to create a string of specific characters with specific lengths in C++. Let’s have a look at the various methods available to get a specific length string with the specific character where the character and the length of the string are taken as user input.

Method 1: In this method, for loop is used. The idea is to first, get the length L of the string is taken as input and then input the specific character C and initialize empty string str. Now, iterate a loop for L number of times. In every iteration, the specific character is concatenated with string str until the for loop ends.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <iostream>
using namespace std;
 
// Driver Code
int main()
{
    // Initialize the specific length
    // and character of string
    int L;
    char C;
 
    // Initialize the empty string
    // for concatenation
    string str = "";
 
    // Input the specific length and
    // character of string
    cin >> L;
    cin >> C;
 
    // Run the for loop L times
    for (int i = 0; i < L; i++)
        str = str + C;
 
    // Output the result string
    cout << str;
 
    return 0;
}


 
 

Output:

 

 

Method 2: In this method, the predefined function append() is used in this method. This function is in the string library in C++. Therefore, the idea is to first initialize the length L, specific character C, and the string str, then input the length of the string that the user wants and the specific character. After that call the append() function with the necessary parameters, and then print the string str.

 

Below is the implementation of the above approach:

 

C++




// C++ program for the above approach
#include <iostream>
using namespace std;
 
// Driver Code
int main()
{
    // Initialize the specific length
    // and character of string
    int L;
    char C;
 
    // Initialize the string
    string str;
 
    // Input the specific length and
    // character of string
    cin >> L;
    cin >> C;
 
    // Call the append function with
    // specific parameters
    str.append(L, C);
 
    // Output the result string
    cout << str;
 
    return 0;
}


 
 

Output:

 

 

Method 3: This method is very easy, fast, and efficient. In this method, the constructor of std::string is being used. Simply input the length L, character C, and initialize the string object with default values.

 

Below is the implementation of the above approach:

 

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Driver Code
int main()
{
    // Initialize the specific length
    // and character of string
    int L;
    char C;
 
    // Input the specific length
    // and character of string
    cin >> L;
    cin >> C;
 
    // Initialize the string object
    // with default values
    string str(L, C);
 
    // Output the result string
    cout << str;
 
    return 0;
}


 
 

Output:

 

 



Similar Reads

Find permutation of numbers upto N with a specific sum in a specific range
Given four numbers N, L, R, and S, the task is to find a permutation of first N natural numbers(1-based indexing) having the sum as S from index L to R. If there are multiple permutations, then print any of them. Otherwise, print -1. Examples: Input: N = 6, L = 3, R = 5, S = 8Output: 3 4 5 2 1 6Explanation: The output permutation has 5 at index L a
15 min read
Lexicographically smallest permutation of a string that can be reduced to length K by removing K-length prefixes from palindromic substrings of length 2K
Given a binary string str of length N, and an integer K, the task is to find the lexicographically smallest permutation of the string str that can be reduced to length K by removal of every K-length prefix from palindromic substrings of length 2K. If no such permutation exists, print "Not Possible". Examples: Input: str = "0000100001100001”, K = 4O
14 min read
Minimize steps to form string S from any random string of length K using a fixed length subsequences
Given a string S consisting of N characters and a positive integer K, the task is to find the minimum number of operations required to generate the string S from a random string temp of size K and inserting the subsequence of any fixed length from the random string in the random string. If it is impossible to generate the string S, then print "-1".
12 min read
Generate string after adding spaces at specific positions in a given String
Given a string s and an array spaces[] describing the original string's indices where spaces will be added. The task is to add spaces in given positions in spaces[] and print the string formed. Examples: Input: s = "GeeksForGeeK", spaces = {1, 5, 10}Output: "G eeks ForGe eK" Explanation: The underlined characters in "GeeksForGeeK" relate to the ind
8 min read
How to Create a 2D Array of a Specific Size and Value in C++?
In C++, a 2D array is an array of arrays that stores data in the form of a grid with rows and columns. In this article, we will learn how to create a 2D array of a specific size and fill it with some value in C++. Example Input: rows = 3; columns = 3; value = 1; Output: 2D Array: { {1, 1, 1}, {1, 1, 1}, {1, 1, 1} }Create a 2D Array of a Specific Si
2 min read
std::string::length, std::string::capacity, std::string::size in C++ STL
Prerequisite: String in C++ String class is one of the features provided by the Standard template library to us, So it comes up with great functionality associated with it. With these Functionalities, we can perform many tasks easily. Let's see a few of the functionalities string class provides. Header File &lt;string&gt; String Functionalities The
6 min read
Minimum length of the sub-string whose characters can be used to form a palindrome of length K
Given a string str consisting of lowercase English letters and an integer K. The task is to find the minimum length of the sub-string whose characters can be used to form a palindrome of length K. If no such sub-string exists then print -1.Examples: Input: str = "abcda", k = 2 Output: 5 In order to form a palindrome of length 2, both the occurrence
14 min read
Construct a string of length L such that each substring of length X has exactly Y distinct letters
Given the length of the string l, the length of the substring x and the number of distinct characters that a substring of length x must have are y, the task is to find a string of length l in which every substring of length x has y distinct characters.Examples: Input: l = 6, x = 5, y = 3 Output: abcabc Explanation: If we take a substring of the fir
4 min read
Minimum length of Run Length Encoding possible by removing at most K characters from a given string
Given a string S of length N, consisting of lowercase English alphabets only, the task is to find the minimum possible length of run-length-encoding that can be generated by removing at most K characters from the string S. Examples: Input: S = "abbbcdcdd", N = 9, K = 2 Output: 5 Explanation: One possible way is to delete both occurrences of 'c' fro
10 min read
Generate an N-length string having longest palindromic substring of length K
Given two integers N and K (K ? N), the task is to obtain a string of length N such that maximum length of a palindromic substring of this string is K. Examples: Input: N = 5, K = 3 Output: "abacd" Explanation: Palindromic substrings are "a", "b", "c", "d" and "aba". Therefore, the longest palindromic substring from the given string is of length 3.
4 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg