C++Primer 习题3.1-3.5

tech2022-07-08  184

C++Primer 习题3.1-3.5

string使用

//3.2.3思考 #include<iostream> using namespace std; int main() { string str{ "123 ab\t c..??" }; for (auto a : str) { //if (isalpha(a)) // cout << a << "是一个字母" << endl; //else if (isdigit(a)) // cout << a << "是一个数字" << endl; //else // cout << a << "是一个其他字符" << endl; int b=(int)++a; cout << b << " "; } cout << endl << __cplusplus; return 0; } //3.2 #include<iostream> #include<string> //必须要有,不然<<,>>有歧义 using namespace std; int main() { string str1; getline(cin, str1); cout << str1 << endl; string str2, str3; while (cin>>str3) { str2 += str3; cout << str2 << endl; } return 0; } //3.4 #include<iostream> #include<string> using namespace std; int main() { string str1; getline(cin,str1); string str2; getline(cin, str2); if (str1.size() == str2.size()) { if (str2 == str1) cout << str1 << endl; else cout << str1 << "与" << str2 << "字符串的长度相同,但不一致" << endl; } else { cout << ((str1 > str2) ? str1 : str2) << endl; } return 0; } //3.5 #include<iostream> #include<string> using namespace std; int main() { string str1,str2; for (; cin >> str2;) { str1 += str2; break; } cout << str1 << endl; string str3, str4; for (; cin >> str4;) { str3 += str4; str3 += " "; break; } return 0; }
最新回复(0)