[字符串]
从天体赛回来之后,发现好多字符串的题呀,字符串都是毒瘤题目。
再PTA上面,gets() 不能用,我用的是
1 char s[110]; 2 cin.get(s,110);
导致这种用字符串string很好写的,硬生生给我弄成了毒瘤
string s; getline(cin,s);
#include<ctype.h>
isalnum:测试字符是否为英文或数字;
isalpha:测试字符是否为英文字母;
isdigit:测试字符是否为阿拉伯数字
islower:测试字符是否为小写字母
isspace:测试字符是否为空格
isupper:测试字符是否大写字母
tolower:把大写字母转成小写字母,不是大写字母的略过。
1 #include<sstream> 2 stringstream ss(s); 3 while(ss>>s) 4 str[cnt++]=s;
substr(index,num)获取index开始,长度为num的子串
erase(index,num)删除index开始num个元素
insert(index,a) index处插入a
replace(index,num,a)从index开始num个元素替换为a字符串
1 pos=kk.find(s1); 2 while(pos!=-1) 3 { 4 kk.replace(pos,s1.size(),s2); 5 pos=kk.find(s1); 6 }
1 string str("ab'defg"); 2 string::iterator it; 3 for (it =str.begin(); it != str.end(); ++it) 4 { 5 if ( *it == '\'') 6 str.erase(it); 7 }
1 #include<iostream> 2 #include<cstdio> 3 #include<string> 4 #include<cstring> 5 #include<map> 6 #include<set> 7 #include<vector> 8 #include<queue> 9 #include<algorithm> 10 #include<cmath> 11 #include<list> 12 #include<cctype> 13 #include<sstream> 14 using namespace std; 15 typedef long long ll; 16 const int inf=0x7fffffff; 17 const int N=100000+100; 18 const int M=9999999; 19 const ll mod=1000000000+7; 20 int n; 21 string s; 22 int main() 23 { 24 scanf("%d",&n); 25 getchar(); 26 while(n--) 27 { 28 string str[1005]; 29 int cnt=0; 30 getline(cin,s); 31 cout<<s<<endl<<"AI:"; 32 for(int i=0;i<s.size();i++) 33 { 34 if(isalnum(s[i])) 35 { 36 if(s[i]!='I') s[i]=tolower(s[i]); 37 } 38 else 39 { 40 s.insert(i," "); 41 i++; 42 } 43 if(s[i]=='?') 44 s[i]='!'; 45 } 46 stringstream ss(s); 47 while(ss>>s) 48 str[cnt++]=s; 49 if(!isalnum(str[0][0])) 50 cout<<" "; 51 for(int i=0;i<cnt;i++) 52 { 53 if(!isalnum(str[i][0])) 54 cout<<str[i]; 55 else if(str[i]=="can"&&str[i+1]=="you") 56 { 57 cout<<" I can"; 58 i++; 59 } 60 else if(str[i]=="could"&&str[i+1]=="you") 61 { 62 cout<<" I could"; 63 i++; 64 } 65 else if(str[i]=="I"||str[i]=="me") 66 cout<<" you"; 67 else 68 cout<<" "<<str[i]; 69 } 70 cout<<endl; 71 } 72 73 return 0; 74 }
天梯赛是将两个独立的单词 i can 修改
月赛是将只要出现了就可以修改
1 #include<iostream> 2 #include<cstdio> 3 #include<string> 4 #include<cstring> 5 #include<map> 6 #include<set> 7 #include<vector> 8 #include<queue> 9 #include<algorithm> 10 #include<cmath> 11 #include<list> 12 #include<cctype> 13 #include<sstream> 14 using namespace std; 15 typedef long long ll; 16 const int inf=0x7fffffff; 17 const int N=100000+100; 18 const int M=9999999; 19 const ll mod=1000000000+7; 20 int n; 21 string s,str[110],kk; 22 string s1="i can't",s2="i can"; 23 string s3="i couldn't",s4="i could"; 24 string s5="duxing201606nb!"; 25 int main() 26 { 27 while(getline(cin,s)) 28 { 29 memset(str,0,sizeof(str)); 30 int cnt=0; 31 for(int i=0;i<s.size();i++) 32 { 33 if(!isalnum(s[i])) 34 { 35 s.insert(i," "); 36 i++; 37 } 38 } 39 stringstream ss(s); 40 while(ss>>s) 41 str[cnt++]=s; 42 kk.clear(); 43 for(int i=0;i<cnt;i++) 44 { 45 if(str[i]=="gao") continue; 46 if(i!=0) 47 if(isalnum(str[i][0])) 48 kk.insert(kk.size()," "); 49 kk.insert(kk.size(),str[i]); 50 } 51 int pos; 52 pos=kk.find(s1); 53 while(pos!=-1) 54 { 55 kk.replace(pos,s1.size(),s2); 56 pos=kk.find(s1); 57 } 58 pos=kk.find(s3); 59 while(pos!=-1) 60 { 61 kk.replace(pos,s3.size(),s4); 62 pos=kk.find(s3); 63 } 64 for(int i=0;i<kk.size();i++) 65 if(kk[i]=='.') 66 kk.insert(i+1,s5); 67 for(int i=0;i<kk.size();i++) 68 cout<<kk[i]; 69 cout<<endl; 70 71 } 72 73 74 return 0; 75 }
No matter how you feel, get up , dress up , show up ,and never give up.