C++分割字符串问题,跪求!

来源:百度知道 编辑:UC知道 时间:2024/05/24 17:02:24
现在要用C++做一个分割字符串的程序。例如,运行程序后出现提示“请输入字符串”字样, 输入字符串后以TBC为分隔符输出TBC前后的字符,比如输入 “1234TBC5678”后,输出 1234
5678

本人是菜鸟,跪求高手指点!非常感谢!

跪求就不用了,给点分就好~~

#include <iostream>
#include <iomanip> // for setw()
#include <string>
using namespace std;

const char SPLIT_STR[] = "TBC";

int main() {
string str;
string::size_type idx, pos, oldpos;

getline(cin, str);
cout << "[ "<< str << " ] -> " << endl;

idx = oldpos = pos = 0;
while ( (pos = str.find(SPLIT_STR, pos)) != string::npos ) {
if (pos - oldpos > 0) {
cout << setw(4) << ++idx << ": " << str.substr(oldpos, pos - oldpos) << endl;
oldpos = pos = pos + sizeof(SPLIT_STR) - 1;
}
}

if (oldpos < str.length()) {
cout << setw(4) << ++idx << ": " << str.substr(oldpos) << endl;
}
}

弄个字符串匹配检