Skip to content

Instantly share code, notes, and snippets.

@kingsamchen
Last active December 31, 2015 03:08
Show Gist options
  • Select an option

  • Save kingsamchen/7925177 to your computer and use it in GitHub Desktop.

Select an option

Save kingsamchen/7925177 to your computer and use it in GitHub Desktop.
/* remove non-alphabet from head and tail */
int main()
{
const char* alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
string text;
while (cin >> text) {
string::size_type begin = text.find_first_of(alphabet);
string::size_type end = text.find_last_of(alphabet) + 1;
text = text.substr(begin, end - begin);
cout << text << endl;
}
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
std::string str = " //*---Hello Word!......------";
auto begin = find_if(str.begin(), str.end(), isalpha);
auto end = find_if(str.rbegin(), str.rend(), isalpha);
str = str.substr(distance(str.begin(), begin), distance(begin, end.base()));
cout << str << endl;
getchar();
return 0;
}
/* user-defined string split */
void SplitString(const string& str, vector<string>* tokens)
{
const char* delim = " |,";
string::size_type begin = 0, end;
while ((begin = str.find_first_not_of(delim, begin)) != string::npos) {
end = str.find_first_of(delim, begin);
if (end == string::npos)
end = str.length();
tokens->push_back(str.substr(begin, end - begin));
begin = end + 1;
}
}
void SplitString(const string& str, char delim, vector<string>* tokens)
{
std::stringstream ss(str);
string item;
while (std::getline(ss, item, delim)) {
tokens->push_back(item);
}
}
int GetLine(const char* buf, size_t offset, size_t bufSize, string* line)
{
int processedCnt = 0;
size_t i;
for (i = offset; i < bufSize; ++i) {
++processedCnt;
if (buf[i] == '\n' || buf[i] == '\0') {
break;
} else if (buf[i] == '\r') {
++processedCnt;
break;
}
}
line->assign(&buf[offset], &buf[i]);
return processedCnt;
}
void StringReplace(string* src, const string& str, const string& rewritten)
{
string::size_type pos = 0;
while ((pos = src->find(str, pos)) != string::npos) {
src->replace(pos, str.length(), rewritten);
pos += rewritten.length();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment