跳到正文

个人记录

字符数组(二)

obbycode约 4 分钟
原文

这是 2026年9月5日 存档的第 4 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年9月5日
收录
2026年9月5日
更新
2026年10月4日
篇幅
约 4 分钟

版本

  1. v5当前
  2. v4
  3. v3
  4. v2
更早的 1 个版本
  1. v1

最长单词2

C++
#include<bits/stdc++.h>
using namespace std;
int main() {
    int maxlen=0;
    bool flag=false;
    char word[500],res[500];
    while(cin>>word)
    {
        int len=strlen(word);
        if(word[len-1]=='.') //判断是否是最后一个单词
        {
            len--;
            word[len]='\0';
            flag=true;
        }
        if(len>maxlen)//比较单词的长度
        {
            maxlen=len;
            strcpy(res,word);//复制
        }
        if(flag) break;
    }
    cout<<res;
}

亲朋字符

C++
#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;
    }
}

验证子串

C++
//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";
}

配对碱基链

C++
#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";
    }
}

连续出现的字符

C++
#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";
}