If I have the following code:
std::string name = "Michael";
std::string spaces = " ";
How would I programatically create the spaces
string (a string with all spaces, the length matching the name variable)?
Naming rulesSpaces are not allowed in variable names, so we use underscores instead of spaces.
In this case, string will be terminated as spaces found, this only "Vanka" will be stored in variable name. Now, how to read string with spaces in C++? We can use a function getline(), that enable to read string until enter (return key) not found.
You can pass a character and a length to a string, and it will fill a string of that length with the given character:
std::string spaces(7, ' ');
You can use the .size()
property of std::string to find the length of your name; combined with the above:
std::string name = "Michael";
std::string spaces(name.size(), ' ');
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