么Question of Computer Science么
Write a program in JAVA to check weather two string are equal or not.
{ Use JAVA }
Answers
Answer:
We can check whether the two strings are equal or not by 3 different ways
- Using equals() : This is case sensitive. Returns false although they are same but Difference is in Capital and small letters.
- Example:- "HELLO".equals("HellO"); //False
- Using equalsIgnoreCase() : This is not case sensitive, therefore returns true even if there is difference in Capital and small letters.
- Example: "Hello".equalsIgnoreCase("HELLO"); //True
- Using CompareTo() : This compares the two strings with ASCII códes of each letters. Returns 0 when they are equal. Returns value <0 when first String is smaller else returns value >0.
//This program is done using equalsIgnoreCase()
import java.util.*;
class Abc{
public static void main (String ar []){
Scanner sc=new Scanner (System.in);
String a=sc.nextLine();
String b=sc.nextLine();
if(a.equalsIgnoreCase(b))
System.out.println("Equal");
else
System.out.println("Not Equal");
}
}
Answer:
Check whether two strings are equivalent or not according to given condition
Given two strings A and B of equal size. Two strings are equivalent either of the following conditions hold true:
1) They both are equal. Or,
2) If we divide the string A into two contiguous substrings of same size A1 and A2 and string B into two contiguous substrings of same size B1 and B2, then one of the following should be correct:
A1 is recursively equivalent to B1 and A2 is recursively equivalent to B2
A1 is recursively equivalent to B2 and A2 is recursively equivalent to B1
Check whether given strings are equivalent or not. Print YES or NO.