zoukankan      html  css  js  c++  java
  • c++关于字符串的读入和截取

    #include<iostream>
    #include<string>
    #include<vector>
    using namespace std;
    vector<string> splitEx(const string& src, string separate_character)
    {
    vector<string> strs;

    int separate_characterLen = separate_character.size();//分割字符串的长度,这样就可以支持如“,,”多字符串的分隔符
    int lastPosition = 0, index = -1;
    while (-1 != (index = src.find(separate_character, lastPosition)))
    {
    strs.push_back(src.substr(lastPosition, index - lastPosition));
    lastPosition = index + separate_characterLen;
    }
    string lastString = src.substr(lastPosition);//截取最后一个分隔符后的内容
    if (!lastString.empty())
    strs.push_back(lastString);//如果最后一个分隔符后还有内容就入队
    return strs;
    }


    int main()
    {
    /*
    string s;
    string sum;
    while (cin >> s){
    sum += s + " ";
    if (cin.get() == ' ')
    break;
    }
    cout << sum << endl;
    */
    int tpp = 1;
    string str;
    do{
    string s;
    string temp;
    cout << "输入一行字符串: ";
    while (cin >> s){
    temp += s + " ";
    if (cin.get() == ' ')
    break;
    }
    cout << "继续输入请按1";
    cout << temp << endl;
    str = temp;
    cin >> tpp;

    } while (tpp==1);


    //string str;
    /*
    int count;//单词个数
    int i, j;
    */

    //cout << "输入一行字符串: ";
    //getline(cin, str);

    /*
    for (count = 0, j = str.size(), i = 0; i<j;)
    {
    while (str[i] == ' '&&i<j) i++;
    if (i<j) count++;
    while (str[i] != ' '&&i<j) i++;
    }
    cout << "j=" << str.size() << endl;
    cout << "输入的字符串为: " << str << endl;
    cout << "字符串中包含的单词数为:" << count << endl;
    */

    string del = " ";
    vector<string> strs = splitEx(str, del);
    //cout << "strsSize=" << strs.size() << endl;
    cout << "输入的字符串为: " << str << endl;
    cout << "字符串中包含的单词数为:" << strs.size() << endl;
    //int a,b,c,d;
    unsigned int i = strs.size()-1;
    while (i >=0)
    //for (unsigned int i = 0; i < strs.size(); i++)
    {
    int j = 1;
    int a = atoi(strs[i].c_str());
    int b = atoi(strs[i-1].c_str());
    int c = atoi(strs[i - 2].c_str());
    int d = atoi(strs[i-3].c_str());
    int cc = atoi(strs[j+2].c_str());
    //cout <<b+1 << endl;
    if (a == b && c == d){
    cout << "yes" << endl;
    break;
    }

    else
    {
    cout << "no" << endl;
    break;
    }
    //i -= 3;
    }


    cout << endl;
    system("pause");
    return 0;


    }

  • 相关阅读:
    XML 浏览器支持
    浏览器中的XML
    C/C++中判断某一文件或目录是否存在
    C/C++程序员必须熟练应用的开源项目 -- 转
    VC 中窗口的销毁
    sql proc触发异常处理回滚
    为Array 添加indexOf
    Js的两种post方式
    sql 针对多个id或名称的分割和组合
    sql 查看语句的性能
  • 原文地址:https://www.cnblogs.com/beihaidao/p/8540874.html
Copyright © 2011-2022 走看看