01:统计数字字符个数
#include "iostream"
#include "iomanip"
using namespace std;
int main()
{
string str;
getline(cin,str);
int count=0;
for(int i=0;i=48&&str.at(i)<=57)
{
count++;
// cout<
02:找第一个只出现一次的字符
#include "iostream"
#include "iomanip"
using namespace std;
int main()
{
string str;
getline(cin,str);
bool flag=false;
int num[26]={0};
for(int i=0;i
03:基因相关性
#include "iostream"
#include "iomanip"
using namespace std;
int main()
{
double num;
cin>>num;
string str1,str2;
cin>>str1>>str2;
int count=0;
for(int i=0;i=num)
cout<<"yes";
else
cout<<"no";
return 0;
}
04:石头剪子布
#include "iostream"
#include "iomanip"
using namespace std;
void cmpStr(string str1,string str2)
{
if(str1=="Rock")
{
if(str2=="Rock")
cout<<"Tie"<>N;
string str1,str2;
for(int i=0;i>str1>>str2;
cmpStr(str1, str2);
}
return 0;
}
05:输出亲朋字符串
#include "iostream"
#include "string"
using namespace std;
int main()
{
string s1,s2;
getline(cin,s1);
//注意:前面定义string类型的s2时是默认初始化,长度为0,所以这里我们指定s2的长度与s1一致
s2.resize(s1.length(),'0');
for(int i=0;i
06:合法 C 标识符
#include "iostream"
#include "string"
using namespace std;
bool isLegal(char c)
{
if(c>='0'&&c<='9'||c=='_'||c>='a'&&c<='z'||c>='A'&&c<='Z')
{
return true;
}
return false;
}
bool isLegal2(char c)
{
if(c=='_'||c>='a'&&c<='z'||c>='A'&&c<='Z')
{
return true;
}
return false;
}
int main()
{
string str;
getline(cin,str);
bool legalF = true;
for(int i=0;i
07:配对碱基链
#include "iostream"
#include "string"
using namespace std;
int main()
{
string str;
getline(cin,str);
for(int i=0;i
08:字符替换
#include "iostream"
#include "string"
using namespace std;
int main()
{
string str;
char C,newC;
cin>>str>>C>>newC;
for(int i=0;i
09:密码翻译
#include "iostream"
#include "string"
using namespace std;
int main()
{
string str;
//注意:有空格的字符出输出用getline不要用cin
getline(cin,str);
for(int i=0;i='a'&&str[i]<='y')||(str[i]>='A'&&str[i]<='Y'))
{
str[i]=str[i]+1;
}
else if(str[i]=='z'||str[i]=='Z')
{
str[i] = str[i]-25;
}
}
cout<
10:简单密码
#include "iostream"
#include "string"
using namespace std;
int main()
{
string str;
//注意:有空格的字符出输出用getline不要用cin
getline(cin,str);
for(int i=0;i'Z')
continue;
str[i] = (str[i]-'A'-5+26)%26+'A';
}
cout<
11:潜伏者
#include "iostream"
#include "string"
using namespace std;
char match[26];
int main()
{
string before,after;
cin>>after>>before;
string text;
cin>>text;
//一个密文字符只能对应一个明文字符
for(int i=0;i
12:加密的病历单
#include "iostream"
#include "string"
using namespace std;
int main()
{
string str;
cin>>str;
//大小写反转
for(int i=0;i='a'&&str[i]<='z')
{
//cout<='A'&&str[i]<='Z')
{
str[i]=str[i]+('a'-'A');
}
}
//cout<='a'&&str[i]<='w')||(str[i]>='A'&&str[i]<='W'))
str[i]=str[i]+3;
else
str[i]=str[i]-26+3;
}
cout<
13:将字符串中的小写字母转换成大写字母
#include "iostream"
#include "string"
using namespace std;
int main()
{
string str;
//可能包含空格
getline(cin,str);
//小写转大写
for(int i=0;i='a'&&str[i]<='z')
{
//cout<
14:大小写字母互换
#include "iostream"
#include "string"
using namespace std;
int main()
{
string str;
getline(cin,str);
//大小写互转
for(int i=0;i='a'&&str[i]<='z')
{
//cout<='A'&&str[i]<='Z')
{
str[i]=str[i]+('a'-'A');
}
}
cout<
15:整理药名
#include "iostream"
#include "string"
using namespace std;
int main()
{
int n;
cin>>n;
for(int k=0;k>str;
if (str[0] >= 'a' && str[0] <= 'z') {
//cout<= 'A' && str[i] <= 'Z') {
str[i] = str[i] + ('a' - 'A');
}
}
cout<
16:忽略大小写的字符串比较
#include "iostream"
#include "string"
using namespace std;
char lower(char c1)
{
if(c1>='A'&&c1<='Z')c1=c1+'a'-'A';
return c1;
}
int main()
{
string str1,str2;
getline(cin,str1);
getline(cin,str2);
int maxLen = str1.length()>str2.length()?str1.length():str2.length();
for(int i=0;i";
}
break;
}
} else {
if (str1[i] == '\0' && str2[i] != '\0'){
cout << "<";
break;
} else if (str1[i] != '\0' && str2[i] == '\0') {
cout << ">";
break;
} else if (str1[i] == '\0' && str2[i] == '\0') {
cout << "=";
break;
}
}
}
}
17:字符串判等
#include "iostream"
#include "string"
using namespace std;
char lower(char c1)
{
if(c1>='A'&&c1<='Z')c1=c1+'a'-'A';
return c1;
}
string dropBlank(string str)
{
string temp;
int blankNum = 0;
for(int i=0;i
18:验证子串
#include "iostream"
#include "string"
using namespace std;
bool AisSubofB(string A,string B)
{
int lenA = A.length();
int indexA = 0;
for(int i=0;i>str1>>str2;
int len1 = str1.length();
int len2 = str2.length();
if(len1<=len2) {
if (AisSubofB(str1, str2))
cout << str1 << " is substring of " << str2;
else
cout << "No substring";
}else
{
if(AisSubofB(str2,str1))
{
cout<
19:字符串移位包含问题
#include "iostream"
#include "string"
using namespace std;
bool AisSubofB(string A,string B)
{
int lenA = A.length();
int indexA = 0;
for(int i=0;i>str1>>str2;
int len1 = str1.length();
int len2 = str2.length();
if(len1<=len2) {
for(int i=0;i
20:删除单词后缀
#include "iostream"
using namespace std;
int main()
{
string word;
cin>>word;
int len = word.length();
if(word[len-2]=='e'&&word[len-1]=='r')
word.erase(word.length()-2,word.length()-1);
else if(word[len-2]=='l'&&word[len-1]=='y')
word.erase(word.length()-2,word.length()-1);
else if(word[len-3]=='i'&&word[len-2]=='n'&&word[len-1]=='g')
word.erase(word.length()-3,word.length()-1);
cout<
21:单词替换
题目有问题,略过
22:紧急措施
#include "iostream"
using namespace std;
string changePwd(string str)
{
for(int i=0;i='a'&&str[i]<='z')
{
str[i] = str[i]-('a'-'A');
}else if(str[i]>='A'&&str[i]<='Z')
{
str[i] = str[i]+('a'-'A');
}
}
return str;
}
int main()
{
string email;
cin>>email;
int num;
cin>>num;
cin.ignore();
bool flag = false;
for(int i=0;i
23:过滤多余的空格
#include "iostream"
using namespace std;
int main()
{
string str;
getline(cin,str);
int blankNum = 0;
for(int i=0;i
24:单词的长度
#include "iostream"
using namespace std;
int main()
{
string str;
getline(cin,str);
int index1 = 0,index2 = 0;
//只有第一次输出时不输出逗号,设置第一次输出的标志位
bool firstF = true;
//从头到尾(包括\0结束符)检查字符串
for(int i=0;i
25:最长最短单词
#include "iostream"
using namespace std;
int main()
{
string str;
getline(cin,str);
string shortWord,longWord;
int shortL=20000,longL = 0;
int index1 = 0,index2 = 0;
//从头到尾(包括\0结束符)检查字符串
for(int i=0;i index2 - index1) {
shortWord = str.substr(index1, index2 - index1);
shortL = index2 - index1;
}
if (longL < index2 - index1) {
longWord = str.substr(index1, index2 - index1);
longL = index2 - index1;
}
//cout<