Need help in getting algorithms

Neon92

Honorary Member
Joined
Mar 4, 2013
Messages
109,970
Reaction score
12,838
dPknhZd.gif


Hi all I have this piece of code that works when users States like 20 characters it will get something like from "Hello world good morning" to
Hello world good
But if the user States 1 to 4 characters i need to be able get Hello
 

cwchong

Master Member
Joined
Jan 7, 2005
Messages
4,654
Reaction score
96
Wah, got lazy until dun even copy paste one...

Im guessing ur program limits the string to at most N chars, and discards partial words?
What u want in addition is such that if N < firstword.length, return firstword?

There, the code is above already
 

Neon92

Honorary Member
Joined
Mar 4, 2013
Messages
109,970
Reaction score
12,838
Here is the code

Code:
std::vector<std::string> StringUtils::WordWrap(const std::string & words, size_t line_length)
	{
		std::vector<std::string> temp;
		std::string tempWords = words;
		bool fullWord = false;
		std::string::size_type i = 0, str_len = words.length();
		while (i < str_len)
		{
			// skip white space
			while (i<str_len && (tempWords[i] == ' ')) { ++i; }

			std::string::size_type j = i;
			
			while (j<str_len && j != line_length + i ) { ++j; }

			while (tempWords[j] != ' ' && j != str_len)
			{
				j--;
			}
			// if we've actually a word ...
			if (i != j) {
				temp.push_back(words.substr(i, j - i));
			}
			i = j;
		}
		return temp;
	}

There are 23 lines wrapped at 20 characters:
Line # 1 [18] |When in the Course|
Line # 2 [19] |of human events, it|
Line # 3 [17] |becomes necessary|
Line # 4 [17] |for one people to|
Line # 5 [12] |dissolve the|
Line # 6 [15] |political bands|
Line # 7 [20] |which have connected|
Line # 8 [18] |them with another,|
Line # 9 [19] |and to assume among|
Line # 10 [17] |the powers of the|
Line # 11 [19] |earth, the separate|
Line # 12 [20] |and equal station to|
Line # 13 [17] |which the Laws of|
Line # 14 [13] |Nature and of|
Line # 15 [20] |Nature's God entitle|
Line # 16 [14] |them, a decent|
Line # 17 [14] |respect to the|
Line # 18 [19] |opinions of mankind|
Line # 19 [18] |requires that they|
Line # 20 [18] |should declare the|
Line # 21 [18] |causes which impel|
Line # 22 [11] |them to the|
Line # 23 [11] |separation.|
Shortest line: 11
Longest line: 20

Not able to do this
There are 11 lines wrapped at 1 characters:
Line # 1 [ 4] |When|
Line # 2 [ 2] |in|
Line # 3 [ 3] |the|
Line # 4 [ 6] |Course|
Line # 5 [ 2] |of|
Line # 6 [ 5] |human|
Line # 7 [ 7] |events,|
Line # 8 [ 2] |it|
Line # 9 [ 7] |becomes|
Line # 10 [ 9] |necessary|
Line # 11 [ 3] |for|
Shortest line: 2
Longest line: 9
 
Last edited:

cwchong

Master Member
Joined
Jan 7, 2005
Messages
4,654
Reaction score
96
After
If(i != j) {
}

Introduce
Else {
/*
If u reach here, means u have not been able to add any words for this line, so just add in the next word u can find
*/
J = i; // reset j to i position to prepare for finding next word
While(tempwords[j] != ' ' && j != str_len) {
++j; // move j until it reach space or end of text
}
J++; // we want to move j to the actual space pos or en od text, so that j-i = len of word
// now there should be a single word between i and j position inclusive, insert that
Temp.push_back(words.substr(i, j - i));
}
 
Important Forum Advisory Note
This forum is moderated by volunteer moderators who will react only to members' feedback on posts. Moderators are not employees or representatives of HWZ Forums. Forum members and moderators are responsible for their own posts. Please refer to our Community Guidelines and Standards and Terms and Conditions for more information.
Top