Solved

formatting text output

Posted on 1998-06-11
7
172 Views
Last Modified: 2010-04-01
I want to output a string but i want to limit width of the column how do i do this?

string = "hello this is a test for width"

I want it to look like this:

cout  << string;

     hello this
     is a test
     for width

I know cout.width (5) will start 5 over but what will stop it at 15 plus i want it to wrap the word to the next line if the whole word won't fit on that line.
0
Comment
Question by:onestar
7 Comments
 
LVL 15

Accepted Solution

by:
Tommy Hui earned 50 total points
ID: 1165663
You'll need to write the code yourself, i.e. manipulating the strings.

Start at the position you want to wrap at. Look at the character at that position. If it is a space, separate the original string into two substrings: the substring before that position and the substring after that position.

If it is not a space, then you'll need to look from that position to the beginning of the string, stopping at the first space. Once you have found a space, use the code from above to break up the string into two substrings.

If you do not find a space (i.e. reached the beginning of the string), then truncate at the desired position.

0
 
LVL 22

Expert Comment

by:nietod
ID: 1165664
There is no built in feature for doing so, you will have to do it "manually" by counting the characters you output and wrapping as needed.

Do you need help implimenting that?
0
 
LVL 22

Expert Comment

by:nietod
ID: 1165665
beat me by minute...
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 84

Expert Comment

by:ozo
ID: 1165666
char *string = "hello this is a test for width";
char *s0 = string;
char *s1;
while( strlen(s0) > 10 && (s1 = strchr(s0,' ')) ){
        char *s2;
        while( (s2 = strchr(s1+1,' ')) && s2-s0 <= 10 ){ s1=s2; }
        *(s0 = s1) = '\n';
}

0
 
LVL 22

Expert Comment

by:nietod
ID: 1165667
That's about half the length I would have come up with!
0
 
LVL 1

Author Comment

by:onestar
ID: 1165668
That works but I need it to start at certain amount of spaces over aswell.  I tried a few things but I could only get the first part over.

Ex.
     hello this
is a test
for width

Any suggestions?
0
 
LVL 1

Author Comment

by:onestar
ID: 1165669
How would I do this if I want to ask the user for the string?  When I tried this it would only put one word per line until the last line which it had two.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now