d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > How To Set Line Width Limit? > Question And S/s Inside
Add Reply New Topic New Poll
Member
Posts: 1,129
Joined: May 25 2013
Gold: 0.11
Sep 2 2014 09:28am
"Write a program that displays the following quotation on the screen, limit the width of your program lines to 100 characters wide."



not sure how to set the width limit.. I googled for about two hours looking lol

This post was edited by Si7c on Sep 2 2014 09:29am
Member
Posts: 9,803
Joined: Jun 28 2005
Gold: 6.67
Sep 2 2014 11:51am
So is it C, C++ or C# question?
Member
Posts: 1,241
Joined: Jun 25 2011
Gold: Locked
Sep 3 2014 01:16am
Quote (Si7c @ Sep 2 2014 05:28pm)
"Write a program that displays the following quotation on the screen, limit the width of your program lines to 100 characters wide."

http://s29.postimg.org/yqxi257cn/Untitled.png

not sure how to set the width limit.. I googled for about two hours looking lol


You could simply use a for loop to iterate on the text chars and have a counter incremented. Whenever the counter reaches 100, insert '\n' in the text where you are in the loop and reset the counter. Do that as a first version and then use the spaces to keep words uncut (replace the nearest previous space by '\n').

This post was edited by m0hawk on Sep 3 2014 01:17am
Member
Posts: 1,129
Joined: May 25 2013
Gold: 0.11
Sep 3 2014 03:57pm
Quote (m0hawk @ Sep 3 2014 03:16am)
You could simply use a for loop to iterate on the text chars and have a counter incremented. Whenever the counter reaches 100, insert '\n' in the text where you are in the loop and reset the counter. Do that as a first version and then  use the spaces to keep words uncut (replace the nearest previous space by '\n').


that's what ill do.. i never thought of using a loop
thank you
Member
Posts: 1,129
Joined: May 25 2013
Gold: 0.11
Sep 3 2014 03:58pm
Quote (KrzaQ2 @ Sep 2 2014 01:51pm)
So is it C, C++ or C# question?


c++
Member
Posts: 9,803
Joined: Jun 28 2005
Gold: 6.67
Sep 3 2014 04:33pm
It's quite simple. Sure, you could make it better, but I don't think there's a point in doing it.

Code
while(quote.size()){
auto chars = min<int>(quote.size(),100);
cout << quote.substr(0,chars) << endl;
quote.erase(begin(quote),begin(quote)+chars);
}

Executed: http://melpon.org/wandbox/permlink/WEMXoroUW7P7jVms
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll