Need own implementation of this function. I can not implement anything competent. Maybe someone has some kind of example of this function.
Closed due to the fact that off-topic participants Alexander Petrov , AK ♦ , Viktorov , cheops , andreymal 19 Dec '17 at 9:17 .
It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:
- " Learning tasks are allowed as questions only on the condition that you tried to solve them yourself before asking a question . Please edit the question and indicate what caused you difficulties in solving the problem. For example, give the code you wrote, trying to solve the problem "- Alexander Petrov, AK, Viktorov, cheops, andreymal
|
3 answers
Bicycle option:
bool schar(const string& str, const char& op) { auto it = str.cbegin(); while (it != str.cend()) { if (*it == op) return true; it++; } return false; } string smerge(string::const_iterator& it1,string::const_iterator& it2,const string& delim) { string ss; while (it1 != it2) { if (!schar(delim,*it1)) ss += *it1; it1++; } return ss; } vector<string> stoken(const string& str,const string& delim) { auto it1 = str.cbegin(); auto it2 = str.cbegin(); vector<string> vec; while (it2 != str.cend()) { if (schar(delim, *it2)) { string s = smerge(it1, it2, delim); if(s.size()) { vec.push_back(s); } } it2++; } vec.push_back(smerge(it1, it2, delim)); return vec; } |
Well, for example, so - simply and unpretentiously:
char * mystrtok(char * str, const char * delim) { static char * last = 0; if (str) last = str; if ((last == 0) || (*last == 0)) return 0; char * c = last; while(strchr(delim,*c)) ++c; if (*c == 0) return 0; char * start = c; while(*c && (strchr(delim,*c)==0)) ++c; if (*c == 0) { last = c; return start; } *c = 0; last = c+1; return start; } |
You can take and see inside glibc
- Please add the code itself to the response (information on the link may be deleted and the answer will lose value). - Nicolas Chabanovsky ♦
- It is unlikely that all links will remove the code. - KoVadim
- @NicolasChabanovsky in this case, I agree with KoVadim. Even if all three repositories move, the links will
/string/strtok.cwhere to look for an example - in the glibc library, in the/string/strtok.cfile. And the library itself can be considered conditionally eternal and not subject to deterioration of links. - Nick Volynkin ♦
|