Blue Bridge Cup test questions basic practice string comparison BASIC-15 JAVA

Foreword

I have been conducting interviews recently. Many of the written code is too lazy to post to the blog. It is now filled in, but there may be fewer comments. If you have any questions, please contact me

Exam questions basic practice string comparison

Resource limit
Time limit: 1.0s Memory limit: 512.0MB

Problem description
  Given two strings consisting only of uppercase or lowercase letters (length is between 1 and 10), the relationship between them is one of the following 4 cases:
  1: The length of the two strings is not equal . 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 The case can only be completely consistent (that is, it does not satisfy Case 2). For example, beijing and BEIjing
  4: the two strings are equal in length, but even if they are not case sensitive, they cannot be made consistent. For example, Beijing and Nanjing
  program to determine which of the four categories the relationship between the two input character strings belongs, and give the number of the category to which it belongs.

Input format
  includes two lines, each line is a string

The output format has
  only one number, indicating the relationship number of these two strings

Sample input
BEIjing
beiJing

Sample output
3

Code for this question

import java.util.Scanner;

public class StringComparison {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str1 = sc.next();
        String str2 = sc.next();
        if (str1.length() != str2.length()) {
            System.out.println(1);
        } else if (str1.equals(str2)) {
            System.out.println(2);
        } else if (str1.toUpperCase().equals(str2.toUpperCase())) {
            System.out.println(3);
        } else {
            System.out.println(4);
        }
    }
}
Published 113 original articles · Liked 105 · Visitors 20,000+

Guess you like

Origin blog.csdn.net/weixin_43124279/article/details/105418977