Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
How to format long inputs from file?
I have an assignment to write a program that reads questions and answers from a file and gives the user a quiz. I wrote that and it works fine. The only problem is that some of the questions/answers are kind of long and it makes the console output look bad,
example:
"_______________ are used to translate each source code instruction into the appropriate machine language instruction."
WILL BE DISPLAYED AS
_______________ are used to translate each source code instruction into the appro
priate machine language instruction.
I'm wondering if there is a function or something I could use that would separate long strings into multiple lines without cutting any words in half.
Forgot to add this is for C++
1 Answer
- RichardLv 75 years agoFavorite Answer
I don't know about C++, but in C, you can use the strstr() function to search for a space character, then replace it with a newline. Start the search again perhaps 40 or 50 characters further on to do the next one. Just make sure you do not run off the end of the buffer.
char buffer[512];
char *ptr = buf;
ptr += 40;
Then search from where ptr is pointing to.
You could use strlen() to find the length of the string.
Just a few thoughts.