Description
练习string
Input
两个字符串 s1,s2 10<=|S1|,|S2|<=10000;
Output
第一行输出s1和s2的拼接字符串,即s1+s2
第二行输出s1,s2两者中字典序较小者
第三行输出s1的从头开始长度为4的子串
Sample Input
abcdesad
dsadsad
Sample Output
abcdesaddsadsad
abcdesad
abcd
这道题几个坑
字典顺序,输出开头长度为4,其实都好说
string可以for(i=0;i<4;i++),string本身就是数组,所以用这种方法可以省心很多
而用数组,需要将后面补一个"\0"
不然后面乱码,但是dev还看不出来
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1, s2;
getline(cin, s1);
getline(cin, s2);
cout << s1 + s2 << endl;;
if (s1 < s2) {
cout << s1 << endl;
}
else
cout << s2 << endl;
char s[5];
if (s1.length() >= 4)
{
s1.copy(s, 4, 0);
s[4] = '\0';
cout << s;
}
else cout << s1;
return 0;
}