蓝桥杯 基础练习 字符串对比 (vip)

版权声明:(整理不易,如本文对您有益,请为我点赞吧!)本文为博主原创文章,转载请附上博文链接! https://blog.csdn.net/Qi2456/article/details/88287454

基础练习 字符串对比

方法一

#include<iostream>
#include<string.h>
using namespace std;
int main()
{
    char s1[20],s2[20];
    cin>>s1;
    cin>>s2;
    int str1,str2;
    str1=strlen(s1);
    str2=strlen(s2);
    if(str1!= str2)
    {
        cout<<"1";
    }
    else if(str1==str2&&strcmp(s1,s2)==0)
    {
        cout<<"2";

    }
    else if(str1==str2&&strcmp(s1,s2)!=0&&strcasecmp(s1,s2)==0 )
    {
        cout<<"3";

    }
    else
        cout<<"4";
//    cout<<strcmp(s1,s2)<<endl;
//    cout<<strcasecmp(s1,s2)<<endl;
    return 0;
}

方法二

#include<bits/stdc++.h>
using namespace std;
int main()
{
     char a[20],b[20];
   cin>>a;
   cin>>b;
    int l1=strlen(a);
    int l2=strlen(b);
    int i;
    int s[100];
    if(l1 != l2)
    {
        printf("1");
    }
    else
    {
        for( i=0; i<l1; i++)
        {
            if(a[i]!=b[i])
            {
                if(a[i]>='a'&&a[i]<='z')
                    a[i]=a[i]-'a'+'A';
                if(b[i]>='a'&&b[i]<='z')
                    b[i]=b[i]-'a'+'A';
                if(a[i]!=b[i])
                {
                    printf("4");
                    return 0;
                }
                else
                {
                    s[i]=1;
                }
            }
            else
            {
                s[i]=0;
            }
        }
        int n=0;
        for( i=0;i<l1;i++)
            n+=s[i];
        if(n>0)
            printf("3");
        else
            printf("2");

    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Qi2456/article/details/88287454
今日推荐