百行代码,信息量超大!
2024-05-05 09:37:22
发布于:上海
12阅读
0回复
0点赞
代码之前先说一下头文件:
<iostream><string><vector>/分割/<algorithm><cctype>/检查/<sstream>/分割/
加入团队吧!加入团队连接url
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<cctype>
#include<sstream>
using namespace std;
int main(){
string s="triangle";
while (true){
string way;
cin >> way;
if (way=="quit") break;
else if (way=="查找"){
string item;
cin >> item;
cout << s.find(item) << endl;
}
else if (way=="子串"){
int start, len;
cin >> start >> len;
cout << s.substr(start, len) << endl;
}
else if (way=="替换"){
int start, len;
string item;
cin >> start >> len >> item;
s.replace(start, len, item);
}
else if (way=="添加"){
string item;
cin >> item;
s.append(item);
}
else if (way=="插入"){
int start;
string item;
cin >> start >> item;
s.insert(start, item);
}
else if (way=="大小"){
string type;
cin >> type;
if (type=="大写") transform(s.begin(), s.end(), s.begin(), ::toupper);
else if (type=="小写") transform(s.begin(), s.end(), s.begin(), ::tolower);
}
else if (way=="检查"){
string type;
cin >> type;
if (type=="空格"){
int i=0;
while (isspace(s[i])) i++;
cout << (i == s.size()) << endl;
}
else if (type=="小写"){
int i=0;
while (islower(s[i])) i++;
cout << (i == s.size()) << endl;
}
else if (type=="大写"){
int i=0;
while (isupper(s[i])) i++;
cout << (i == s.size()) << endl;
}
else if (type=="数字"){
int i=0;
while (isdigit(s[i])) i++;
cout << (i == s.size()) << endl;
}
else if (type=="字母"){
int i=0;
while (isalpha(s[i])) i++;
cout << (i == s.size()) << endl;
}
else if (type=="字符"){
int i=0;
while (isalnum(s[i])) i++;
cout << (i == s.size()) << endl;
}
}
else if (way=="修改"){
int index;
char item;
cin >> index >> item;
s[index] = item;
}
else if (way=="分割"){
char item;
cin >> item;
stringstream ss; ss<<s;
vector<string> sits;
string f;
while (getline(ss,f,item)) sits.push_back(f);
for (int i=0; i<sits.size(); i++) cout << sits[i] << " ";
cout << endl;
}
}
cout << s << endl;
return 0;
}
全部评论 1
这位兄台说的非常好!
2024-01-01 来自 上海
0
有帮助,赞一个