《算法笔记》3.6小节——入门模拟->字符串处理
1009 说反话
#include <cstdio>
int main() {
char sen[80][80];
int num=0;
while(scanf("%s",sen[num])!=EOF){
num++;
}
for (int i = num-1; i > 0; --i) {
printf("%s ",sen[i]);
}
printf("%s\n",sen[0]);
return 0;
}
字符串连接
#include <iostream>
using namespace std;
int main() {
string a,b;
while(cin>>a>>b){
cout<<a<<b<<endl;
}
return 0;
}
首字母大写
#include <iostream>
#include <cctype>
using namespace std;
bool is_blank(char i){
if(i==' '||i=='\t'||i=='\r'||i=='\n') return true;
else return false;
}
int main() {
string str;
while(getline(cin,str)){
bool is_start=true;
for (int i = 0; i < str.length(); ++i) {
if(is_blank(str[i])) is_start= true;
else if(is_start){
if(islower(str[i])){
str[i]= toupper(str[i]);
}
is_start= false;
}
}
cout<<str<<endl;
}
return 0;
}
字符串的查找删除
参考链接:
https://cloud.tencent.com/developer/ask/sof/110559246
- isspace( )判断字符c是否为空白符,空白符指空格、水平制表、垂直制表、换页、回车和换行符
- 注意大小写比较和输出
#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>
using namespace std;
string removeSpace(const string& input){
string res;
for(char c:input){
if(c!=' ') res.push_back(c);
}
return res;
}
int main() {
string shortStr,inputStr;
getline(cin,shortStr);
transform(shortStr.begin(), shortStr.end(), shortStr.begin(), ::tolower);
while(getline(cin,inputStr)){
if(shortStr.empty()){
cout<<inputStr;
return 0;
}
string temp=inputStr;
transform(temp.begin(),temp.end(),temp.begin(),::tolower);
size_t pos=0;
while((pos=temp.find(shortStr,pos))!=string::npos){
temp.erase(pos,shortStr.length());
inputStr.erase(pos,shortStr.length());
}
string resStr= removeSpace(inputStr);
cout<<resStr<<endl;
}
return 0;
}
单词替换
#include <iostream>
#include <string>
using namespace std;
int main() {
string str,str1,str2;
while(getline(cin,str)){
getline(cin,str1);
getline(cin,str2);
size_t pos=0;
while((pos=str.find(str1,pos))!=string::npos){
str.replace(pos,str1.length(),str2);
pos=pos+str2.length();
}
cout<<str<<endl;
}
return 0;
}
字符串去特定字符
#include <iostream>
using namespace std;
int main() {
string s,c;
while(getline(cin,s)){
getline(cin,c);
if(c.empty()){
cout<<s<<endl;
continue;
}
size_t pos=0;
while((pos=s.find(c,pos))!= string::npos){
s.erase(pos,c.length());
}
cout<<s<<endl;
}
return 0;
}
数组逆置
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main() {
string str;
while(getline(cin,str)){
reverse(str.begin(), str.end());
cout<<str<<endl;
}
return 0;
}
比较字符串
#include <iostream>
using namespace std;
int main() {
int m;
string str1,str2;
cin>>m;
for (int i = 0; i < m; ++i) {
cin>>str1>>str2;
if(str1.length()>str2.length()){
cout<<str1<<" is longer than "<<str2<<endl;
}else if(str2.length()>str1.length()){
cout<<str1<<" is shorter than "<<str2<<endl;
}else{
cout<<str1<<" is equal long to "<<str2<<endl;
}
}
return 0;
}
【字符串】回文串
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
string str;
getline(cin,str);
string tempStr=str;
reverse(tempStr.begin(),tempStr.end());
if(tempStr==str) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
return 0;
}
编排字符串
#include <iostream>
#include <queue>
#include <string>
using namespace std;
int main() {
int m;
cin>>m;
deque<string> names;
string str;
for (int i = 0; i < m; ++i) {
cin>>str;
names.push_front(str);
if(names.size()>4){
names.pop_back();
}
int count=1;
for(const auto&name:names){
cout<<count<<'='<<name;
if(count<names.size()){
cout<<' ';
}
count++;
}
cout<<endl;
}
return 0;
}