Thread: sentence word breaks...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    132

    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?
    Y3K Network http://www.y3knetwork.com
    Bringing the software of the future on the net.

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    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.

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    132
    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?
    Y3K Network http://www.y3knetwork.com
    Bringing the software of the future on the net.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    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.

  5. #5
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  2. Random word problem
    By goron350 in forum C++ Programming
    Replies: 2
    Last Post: 05-14-2005, 03:44 PM
  3. Wrong Output
    By egomaster69 in forum C Programming
    Replies: 7
    Last Post: 01-28-2005, 06:44 PM
  4. Search Sentence for Word?
    By drdroid in forum C++ Programming
    Replies: 4
    Last Post: 02-20-2003, 01:15 PM
  5. Word Count
    By simple in forum C Programming
    Replies: 12
    Last Post: 10-04-2002, 10:47 PM