Oct 5, 2014 at 1:39am UTC
im new to c++ ,so my question is how do i change a length of a text.
for example
hi my name is blah blah blah. nice to meet you.
(n i want every lines to have 8 chars how do i do that)
(hi__my__
name_is_
balh____
blah____
blah____
._nice__
to_meet_
you.) (_ equal space)
Oct 5, 2014 at 3:19am UTC
You are asking a lot here, considering you have no code. This better not be homework.
But if you are not talking about word wrapping, because non you your words are split up in your example, this is how you could do it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
int main()
{
std::string str;
std::getline(std::cin,str);
std::vector<std::string> chunks;
std::stringstream ss;
int i = 0;
for (auto it = str.begin();it != str.end(); it++ )
{
ss<<(*it);
if (i%8 == 7){
chunks.push_back(ss.str());
ss.str("" );
}
++i;
}
chunks.push_back(ss.str()); //the last bit
for (auto it = chunks.begin(); it!=chunks.end();it++)
std::cout<<(*it)<<"\n" ;
return 0;
}
There could be a even more shorter way to do it, using the tools of string (substr, and assign ) but i find this to be a bit more readable.
Last edited on Oct 5, 2014 at 3:59am UTC
Oct 5, 2014 at 3:47pm UTC
@jinjack33
And what would you want done if a word or two had more than 8 characters in it?