fork download
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5.  
  6. string getFirstPart(const string& input)
  7. {
  8. static const char* whitespaces = "\t\n\v\f\r ";
  9. string::size_type begin = input.find_first_not_of(whitespaces);
  10. if(begin == string::npos)
  11. begin = 0;
  12. string result = input.substr(begin, input.find('#') - begin);
  13. string::size_type end = result.find_last_not_of(whitespaces);
  14. if(end != string::npos)
  15. result.erase(end + 1);
  16. return result;
  17. }
  18.  
  19.  
  20. int main()
  21. {
  22. cout << '"' << getFirstPart("") << '"' << endl;
  23. cout << '"' << getFirstPart("abc") << '"' << endl;
  24. cout << '"' << getFirstPart(" a b c ") << '"' << endl;
  25. cout << '"' << getFirstPart("#") << '"' << endl;
  26. cout << '"' << getFirstPart(" #abc") << '"' << endl;
  27. cout << '"' << getFirstPart(" fo o #bar") << '"' << endl;
  28. cout << '"' << getFirstPart(" fo o#bar") << '"' << endl;
  29. cout << '"' << getFirstPart("fo o#") << '"' << endl;
  30. cout << '"' << getFirstPart("fo o #bar") << '"' << endl;
  31. cout << '"' << getFirstPart("\rfoo\n#bar#baz") << '"' << endl;
  32. }
Success #stdin #stdout 0.01s 2864KB
stdin
Standard input is empty
stdout
""
"abc"
"a b c"
""
""
"fo o"
"fo o"
"fo o"
"fo o"
"foo"