当前位置:网站首页>Blue Bridge Cup basic-15 VIP question string comparison

Blue Bridge Cup basic-15 VIP question string comparison

2022-06-12 19:56:00 Hua Weiyun

test questions Based on practice String comparison

Resource constraints
The time limit :1.0s   Memory limit :512.0MB
Problem description
   Given two strings consisting of only uppercase or lowercase letters ( Length between 1 To 10 Between ), The relationship between them is as follows 4 One of the situations in :
  1: Two strings are not the same length . such as Beijing and Hebei
  2: Two strings are not only equal in length , And the characters in the corresponding positions are exactly the same ( Case sensitive ), such as Beijing and Beijing
  3: Two strings are equal in length , The characters in the corresponding positions can be completely consistent only if they are not case sensitive ( in other words , It's not satisfied with the situation 2). such as beijing and BEIjing
  4: Two strings are equal in length , But even case insensitive doesn't make the two strings consistent . such as Beijing and Nanjing
   Programming determines which of these four categories is the relationship between the two input strings , Give the number of the class .
Input format
   It includes two lines , Each line is a string
Output format
   There is only one number , Indicates the relationship number of these two strings
The sample input
BEIjing
beiJing 
Sample output
3
The solution is as follows --

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String str1 = scanner.next();
        String str2 = scanner.next();
        
        if (str1.length() != str2.length()) {            //1: Two strings are not the same length .
            System.out.println(1);
        } else {
            if (str1.equals(str2)) {                    //2: Two strings are not only equal in length , And the characters in the corresponding positions are exactly the same ( Case sensitive )
                System.out.println(2);
            }else if (str1.equalsIgnoreCase(str2)) {    //3: Two strings are equal in length , The characters in the corresponding positions can be completely consistent only if they are not case sensitive
                System.out.println(3);
            }else {                                        //4: Two strings are equal in length , But even case insensitive doesn't make the two strings consistent .
                System.out.println(4);
            }
        }
    }
}

The demonstration effect is as follows --

I hope this article can help you !

Thank you. !

原网站

版权声明
本文为[Hua Weiyun]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202281524042954.html