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!
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.
An empty string ( "" ) consists of no characters followed by a single string terminator character - i.e. one character in total.
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.
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.
#include <string>
std::string mystring(5,' ');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With