Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a string of blanks in C++?

Tags:

c++

string

I need to create a string of blanks in c++, where the number of spaces is a variable, so I can't just type it in. How do I do this without looping ?

Thanks!

like image 848
MLP Avatar asked Jul 16 '10 04:07

MLP


People also ask

How do you make a string empty?

a[0] = '\0'; This sets the first char in the string to be the null terminating character, such that when you print the string, it prints an empty string.

What is the empty string in C?

An empty string ( "" ) consists of no characters followed by a single string terminator character - i.e. one character in total.

How do you declare a string in C?

Declaring a string is as simple as declaring a one-dimensional array. Below is the basic syntax for declaring a string. char str_name[size]; In the above syntax str_name is any name given to the string variable and size is used to define the length of the string, i.e the number of characters strings will store.


2 Answers

size_t size = 5; // size_t is similar to unsigned int ‡
std::string blanks(size, ' ');

See: http://www.cplusplus.com/reference/string/string/string/

‡ See the question on size_t if this isn't clear.

like image 63
Brendan Long Avatar answered Oct 04 '22 06:10

Brendan Long


#include <string>
std::string mystring(5,' ');
like image 22
cape1232 Avatar answered Oct 04 '22 05:10

cape1232