#459. 字符串函数

字符串函数

  1. 如果执行下面C++代码后,输出的结果是"gesp.ccf.org.cn",则横线上应填入哪个代码?( )
#include <iostream>
using namespace std;

int main() {
    string str = "gesp.ccf.org.cn";
    string delimiter = ".";
    string result="";
    string token;
    size_t found = str.find(delimiter);
    while (found != string::npos) {
        token = str.substr(0, found);
        result += token;
        result += " ";
        _______________ // 在此处填入代码
        found = str.find(delimiter);
    }
    result += str;
    result += " ";
    cout << result << endl;
    return 0;
}

{{ select(1) }}

  • str = str.substr(found + delimiter.length(), str.length() - 1);
  • str = str.substr(found, str.length());
  • str = str.substr(found, str.length());
  • 以上都不对
  1. 下列程序输出的是3。( )
string str="CHADai";
size_t pos = str.find('D');
cout<<pos<<endl;

{{ select(2) }}

  1. 下面程序最后能够得到HelloC++的是()
int main(){
    string str = "HelloWorld";
    __________________________
    cout << str;
    return 0;
}

{{ select(3) }}

  • str.replace(0, 5, "C++");
  • str.replace(5, 5, "C++");
  • str.replace(1, 5, "C++");
  • str.replace(4, 5, "C++");
  1. 想要得到字符串world,下面程序横线处应该填入的是()
#include <iostream>
#include <string>
using namespace std;
int main() {
    string str = "HelloC++";
    ______________________________
    ______________________________
    return 0;
}

{{ select(4) }}

  • str.insert(4, "World"); cout << str.substr(4, 4);
  • cout << str.substr(5, 5);
  • str.insert("World");cout << str.substr(5, 5);
  • str.insert(5, "World"); cout << str.substr(5, 5);
  1. 下列代码输出的是
string s="1234@@chenadai";
string str="12345";
s.replace(1,5,str);
cout<<s<<endl;

{{ select(5) }}

  • 12345
  • 2345@
  • 112345chenadai
  • 12345chenadai
  1. 下列可执行程序段中,最后 pos 的值是4
string str="chenADai";  
int pos = str.find('D');  
--pos;  

{{ select(6) }}

  1. C++ 语句 string s="Gesp Test"; 执行 s.rfind("e") 以后,输出的是( ) {{ select(7) }}
  • 1
  • 2
  • 3
  • 6
  1. string 的 substr(1, 3) 返回从下标 11 开始的 33 个字符的子串。( ) {{ select(8) }}
  1. stringsubstr(2, 10) 在字符串长度不足时会抛出异常。( ) {{ select(9) }}
  1. 执行下面C++代码后将输出 22
int main()
{
    string str="gEsP is Interesting";
    int x = str.find("s");
    cout << x << endl;
    cout << endl;
    return 0;
}

{{ select(10) }}

  1. 执行下面C++代码后输出的是( )。
string str = ("chen");
int x = str.length();
cout << x << endl;

{{ select(11) }}

  • 2
  • 3
  • 4
  • 5
  1. 执行下面C++代码后将输出"China"。( )
string a = "china";
a.replace(0, 1, "C");
cout << a << endl;

{{ select(12) }}