版本比较
字符数组(二)
标题
增加 1 行删除 1 行
11字符数组2字符数组(二)正文
增加 97 行删除 11 行
11234567891011121314815161711121819201415162122232418252620212728292330313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113**最长单词2**# 最长单词2```cpp#include<bits/stdc++.h>using namespace std;int main() { // string s,word; // getline(cin,s); // stringstream ss(s); // while(ss>>word) // { // cout<<word<<endl; // } int maxlen=0; bool flag=false; char word[500],res[500]; char word[500],res[500];//word每次输入的单词 res最长单词 while(cin>>word) { int len=strlen(word); if(word[len-1]=='.') //判断是否是最后一个单词 int len=strlen(word);//获取输入单词/字符数组的长度 if(word[len-1]=='.') //如果单词最后面是‘.’,长度-1 { len--; word[len]='\0'; flag=true; len--;//长度-1 word[len]='\0';//‘.’换成‘\0’,字符数组结束的标记 flag=true;//标记输入结束 } if(len>maxlen)//比较单词的长度 if(len>maxlen)//擂台比较//当前长度比之前长 { maxlen=len; strcpy(res,word);//复制 maxlen=len;//替换当前最长长度 strcpy(res,word);//最长单词给res 复制 } if(flag) break; if(flag) break;//结束输入 } cout<<res;}```
# 亲朋字符```cpp#include<bits/stdc++.h>using namespace std;int main(){ char c[105]; cin>>c; int len=strlen(c); for(int i=0;i<len;i++) { int x=(int)(c[i]);//强制转换为整数 int y=(int)(c[(i+1)%len]); char c=(char)(x+y);//强制转换为字符 cout<<c; }}```# 验证子串```cpp//strstr(a,b);a是否包含有b b是否是a的字串 返回地址 NULL#include<bits/stdc++.h>using namespace std;int main(){ char s1[50],s2[50]; cin>>s1>>s2; if(strstr(s2,s1)) cout<<s1<<" is substring of "<<s2; else if(strstr(s1,s2)) cout<<s2<<" is substring of "<<s1; else cout<<"No substring";}```# 配对碱基链```cpp#include<bits/stdc++.h>using namespace std;char c[300];int main(){ cin>>c; int len=strlen(c); for(int i=0;i<len;i++) { if(c[i]=='A') cout<<"T"; else if(c[i]=='T') cout<<"A"; else if(c[i]=='G') cout<<"C"; else cout<<"G"; }}```# 连续出现的字符```cpp#include<bits/stdc++.h>using namespace std;char ch[1005];int main(){ int k,ans=1; cin>>k>>ch; for(int i=0;i<strlen(ch)-1;i++) { if(ch[i]==ch[i+1]) ans++; else { if(ans>=k) { cout<<ch[i]; return 0; } else ans=1; } } cout<<"No";}```