Create a string of specific length in C++ Last Updated : 16 Jul, 2021 Comments Improve Suggest changes Like Article Like 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: Comment More infoAdvertise with us Next Article Create a string of specific length in C++ B bhanusinghpratap37 Follow Improve Article Tags : Strings Competitive Programming C++ Programs Programming Language C++ DSA CPP-Basics +3 More Practice Tags : CPPStrings Similar Reads How to Create a Stack of Strings in C++? In C++, Stacks are a type of container adaptor with a LIFO(Last In First Out) type of working, where a new element is added at one end (top) and an element is removed from that end only. In this article, we will learn how to create a stack of strings in C++. Creating a Stack of Strings in C++To crea 1 min read How to Convert wstring to int in C++? In C++, std::wstring is a type of string where each character is of a wide character type. These types of strings can be used to store the numerical strings which can then be converted to their corresponding type. In this article, we will see how to convert a wstring to an int in C++. Input: wstr = 2 min read How to Split a String into an Array in C++? In C++, splitting a string into an array of substrings means we have to parse the given string based on a delimiter and store each substring in an array. In this article, we will learn how to split a string into an array of substrings in C++. Example: Input: str= âHello, I am Geek from geeksforgeeks 2 min read How to Convert a Single Character to String in C++? A string is a generally made up of a sequence of multiple characters. In this article, we will learn how to convert a single character to a string in C++.Example:Input: c = 'A'Output: s = "a"Explanation: Character c is converted to a string.Input: c = '#'Output: s = "#"Explanation: Character c is co 4 min read Convert character array to string in C++ This article shows how to convert a character array to a string in C++. The std::string in c++ has a lot of inbuilt functions which makes implementation much easier than handling a character array. Hence, it would often be easier to work if we convert a character array to string. Examples: Input: ch 4 min read How to Find the Length of a Substring in a char Array? In C++, a substring is a string inside another string that is the contiguous part of the string. In this article, we will learn how to find the length of a substring in a char array in C++. Example Input: str[] = "Hello, World!";substr= "World";Output: Length of substring World is: 5Finding Length o 2 min read How to Create a Random Alpha-Numeric String in C++? Creating a random alpha-numeric string in C++ means generating random characters from the set of alphanumeric characters (i.e., âAâ-âZâ, âaâ-âzâ, and â0â-â9â) and appending them to a string. In this article, we will learn how to create a random alpha-numeric string in C++. Example Output:a8shg1laCre 2 min read How to Check if a String is Empty in C++? In C++, strings are the sequence of characters that are stored as std::string class objects. In this article, we will learn how to check if a string is empty in C++ Example Input: str1="Hello! Geek" ; str2="" Output: str1 is not empty str2 is emptyChecking if the String is Empty in C++To check for a 2 min read How to Concatenate Multiple Strings in C++? In C++, to concatenate multiple strings means we need to combine two or more strings into a single string. In this article, we will learn how to concatenate multiple strings in C++. Example Input: str1="Hello!" str2="Geek," str3="Happy Coding." Output: Hello! Geek, Happy Coding.Concatenate Multiple 2 min read String Concatenation in C++ String concatenation refers to the process of combining two or more strings into a single string. Generally, one string is appended at the end of the other string.The simplest method to concatenate two strings in C++ is by using + operator. Let's take a look at an example:C++#include <bits/stdc++ 3 min read Like