-
sentence word breaks...
Hello,
I have the user entering text of unknown length. But after a certain sentence width, I must break the sentence, using an \n character, to move below from the start. I am using std::string. My problem isn't on the line break, but on knowing not to separate words. Right now, it breaks everywhere, making the whole thing a mess. I need words to be intact. Any ideas?
-
What if you read one word at a time and check its size to see if it will make the current line too long? When you read in a word that does make the line too long, you add your '\n' and add that word to the next line.
-
Sounds like what I am trying to do. But how to I determine if it is a word or just a combination of characters and spaces?
-
define a word to be a nonwhite space char input followed by a space char (newline char?). Unless yo have a dictionary available to the program that can screen out things like d^&*# or yztr they will be considered words. You could probably screen for non-alphabet input by using isalph() or isalphanum() if you want. But that still won't pick up yztr that yztr isn't a normal word in English, unless you have a dictionary.
You could have a string called token, and a string called line, each with capacity of 81 char. Enter each char of user input into token until a whitespace char is encountered or until 80 char have been input. If token.size == line.capacity, then token and line will be the same. Otherwise, determine the sum of ccurrent line.size and token.size. It result is less than line.capacity, concatenate token to end of line. If result is greater than line.capacity then current line is written to screen, or file, or whatever; the line buffer is cleared and the current token becomes the first token in the new line.
-
I guess it depends on how you are doing it, and what is allowed. I was assuming that you were using cin >> string to get your input, but I can see how that would cause a problem if you didn't know how many words the user was inputting. I assume now that you are using std::getline to get the entire user input up to a newline, or something like that.
If it is ok to change all whitespace in the input to a single space character in the output, I can see how using a stringstream and a loop could work.
Also, you could use the std::string functions find_first_of, find_first_not_of, and substr to tokenize the string but still remember exactly what whitespace was used. I'm sure there might be even better ways, but either of those solutions might work for you.