#459. 字符串函数
字符串函数
- 如果执行下面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());- 以上都不对
- 下列程序输出的是3。( )
string str="CHADai";
size_t pos = str.find('D');
cout<<pos<<endl;
{{ select(2) }}
- 对
- 错
- 下面程序最后能够得到
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++");
- 想要得到字符串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);
- 下列代码输出的是
string s="1234@@chenadai";
string str="12345";
s.replace(1,5,str);
cout<<s<<endl;
{{ select(5) }}
- 12345
- 2345@
- 112345chenadai
- 12345chenadai
- 下列可执行程序段中,最后 pos 的值是4
string str="chenADai";
int pos = str.find('D');
--pos;
{{ select(6) }}
- 对
- 错
- C++ 语句
string s="Gesp Test";执行s.rfind("e")以后,输出的是( ) {{ select(7) }}
- 1
- 2
- 3
- 6
- string 的
substr(1, 3)返回从下标 开始的 个字符的子串。( ) {{ select(8) }}
- 对
- 错
string的substr(2, 10)在字符串长度不足时会抛出异常。( ) {{ select(9) }}
- 对
- 错
- 执行下面C++代码后将输出 。
int main()
{
string str="gEsP is Interesting";
int x = str.find("s");
cout << x << endl;
cout << endl;
return 0;
}
{{ select(10) }}
- 对
- 错
- 执行下面C++代码后输出的是( )。
string str = ("chen");
int x = str.length();
cout << x << endl;
{{ select(11) }}
- 2
- 3
- 4
- 5
- 执行下面C++代码后将输出"China"。( )
string a = "china";
a.replace(0, 1, "C");
cout << a << endl;
{{ select(12) }}
- 对
- 错