--- tags: C/C++ --- [toc] # Randori Kata : 題目 WordWrap ### Description * You write a class called ==Wrapper==, that has a single member function named ==wrapper== that takes two arguments, a string, and a column number. The function returns the string, but with line breaks inserted at just the right places to make sure that no line is longer than the column number. You try to break lines at word boundaries." ### Limit * recursive wrap(std::string s, int col) * use substr() ### Examples ```cpp= // group1: no space wrapper("", 1) => "" wrapper("w", 1) => "w" wrapper("word", 4) => "word" wrapper("word", 10) => "word" wrapper("word", 2) => "wo\nrd" wrapper("wordword", 2) => "wo\nrd\nwo\nrd" // group2: one space between with words wrapper("word word", 5) => "word\nword" wrapper("word word", 6) => "word\nword" wrapper("word word", 3) => "wor\nd\nwor\nd" wor d wor d wrapper("word word", 4) => "word\nword" wrapper("word word", 2) => "wo\nrd\nwo\nrd" wo rd wo rd // group3: more spaces between with words wrapper("word word word", 2) => "wo\nrd\nwo\nrd\nwo\nrd" wo rd wo rd wo rd wrapper("word word word", 10) => "word word\nword" word word word wrapper(" word word word", 11) => " word word\nword" ``` ### substr() * string substr (size_t pos, size_t len) const; ```cpp Parameters: pos: Position of the first character to be copied. len: Length of the sub-string. size_t: It is an unsigned integral type. Return value:  It returns a string object. ``` * Example: ```cpp string a="123456789"; cout<<a.substr(2,5)<<endl; //表示呼叫「字串a索引2數起的5個字元」所構成的子字串,顯示34567 cout<<a.substr(2)<<endl; //表示呼叫「字串a索引2數起之後的所有字元」所構成的子字串,顯示3456789 ``` ### find_last_of() * size_t find_last_of (const string& str, size_t pos = npos) const noexcept; ```cpp= size_t find_last_of (const string& str, size_t pos = npos) const noexcept; Parameters str − It is a string object. pos − Position of the first character to be copied. Return Value The position of the last character that matches. If no matches are found, the function returns string::npos. ``` * Example: ```cpp #include <string> #include <iostream> void print_str(const std::string title, const std::string &str, int pos) { std::cout << title << " = "<< pos << " " << str.substr(0, pos) << " remaining str = " << str.substr(pos) << std::endl; } int main() { const std::string strPath = "E:\\2017\\2018\\2019.txt"; /* 最後一個 \\ , 從0算起, 第12個位置 */ int nPos1 = strPath.find_last_of("\\"); int nPos2 = strPath.find_last_of("\\", nPos1); /* nPos1 是 pos, 找 0 ~ 12 position */ int nPos3 = strPath.find_last_of("\\", nPos1 -1); /* nPos1 是 pos, 找 0 ~ 11 position */ int nPos4 = strPath.substr(0, nPos1).find_last_of("\\"); /* 帶入的 nPos1 是 len */ int nPos5 = strPath.substr(nPos1).find_last_of("\\"); /* 起始位置 nPos1 */ int nPos6 = strPath.substr(nPos1+1).find_last_of("\\"); /* 起始位置 nPos1 + 1 */ print_str("nPos1", strPath, nPos1); print_str("nPos2", strPath, nPos2); print_str("nPos3", strPath, nPos3); print_str("nPos4", strPath, nPos4); std::cout << "nPos5 = " << nPos5 << std::endl; std::cout << "nPos6 = " << nPos6 << std::endl; return 0; } ``` 輸出 ```cpp= nPos1 = 12 E:\2017\2018 remaining str = \2019.txt nPos2 = 12 E:\2017\2018 remaining str = \2019.txt nPos3 = 7 E:\2017 remaining str = \2018\2019.txt nPos4 = 7 E:\2017 remaining str = \2018\2019.txt nPos5 = 0 nPos6 = -1 ``` ### Practice 1: non-recursive WordWrap ### Practice 2: recursive WordWrap