String comparison [蓝桥杯]

Topic link: String comparison
time limit: 1 Sec Memory limit: 256 MB

Title description:
Given two strings consisting of only uppercase or lowercase letters (with a length between 1 and 10), the relationship between them is one of the following 4 cases:
  1: The length of the two strings is not Wait. For example, Beijing and Hebei
  2: The two strings are not only equal in length, but the characters in the corresponding positions are exactly the same (case sensitive), such as Beijing and Beijing
  3: The two strings are equal in length, and the characters in the corresponding positions are only indistinguishable Only under the premise of uppercase and lowercase can complete consistency be achieved (that is, it does not satisfy Case 2). For example, beijing and BEIjing
  4: the two strings are equal in length, but even if it is case-insensitive, the two strings cannot be the same. For example, Beijing and Nanjing
  program to determine which of these four types the relationship between the two input strings belongs to, and give the number of the type.
Input:
consists of two lines, each line is a string
Output:
there is only one number, indicating the relationship number of the two strings

Sample input:
BEIjing
beiJing
Sample output:
3

The meaning of the title: The meaning of the title is to compare two strings to see which category they belong to, and output the relation number representing the string.
Idea: Just compare, start with the length comparison, the third requirement is to ignore the case to determine whether they are equal, we borrow two temporary strings, convert the original two strings to lowercase or uppercase and compare them. But (I converted to lowercase).

Insert picture description here
On the code:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    
    
    string s1,s2;
    while(cin>>s1>>s2)
    {
    
    
        //cout<<s1<<endl;
        //cout<<s2<<endl;
        string ss1,ss2; // 临时的字符串,存放装换为小写的字符串
        ss1=s1;
        ss2=s2;
        int l1=s1.length();
        int l2=s2.length();
        for(int i=0; i<l1; i++)
        {
    
    
            if(s1[i]>='A'&&s1[i]<='Z')
            {
    
    
                ss1[i]=s1[i]+32; //转换为小写
            }
        }
        for(int i=0; i<l2; i++)
        {
    
    
            if(s2[i]>='A'&&s2[i]<='Z')
            {
    
    
                ss2[i]=s2[i]+32;
            }
        }
        //cout<<ss1<<endl;
        //cout<<ss2<<endl;
        if(l1!=l2)
            cout<<1<<endl;
        else if(s1==s2)
            cout<<2<<endl;
        else if(ss1==ss2)
            cout<<3<<endl;
        else
            cout<<4<<endl;
    }
    return 0;
}

Don’t forget to like it after reading, thank you for your support!
If you are on a computer terminal, you can also see the "one key triple connection" in the lower right corner, the icon is as follows, click it right [haha]


Thanks for your support!
Insert picture description here
Come on!

Work together!

Keafmd

Guess you like

Origin blog.csdn.net/weixin_43883917/article/details/108873271