wap to read two strings from user and display both are equal or not using equal and equalsIgnorCase() using Java program
Answers
Solution:
This is written in Java -
import java.util.*;
public class Java_String{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
String a,b;
System.out.print("Enter first String: ");
a=sc.nextLine();
System.out.print("Enter second String: ");
b=sc.nextLine();
if(a.equalsIgnoreCase(b))
System.out.println("Equal.");
else
System.out.println("Not equal.");
sc.close();
}
}
- The equalsIgnoreCase() checks whether two strings are equal or not ignoring the cases. So, strings - "ABCD" and "abcd" will be considered equal.
- However, the equals() method checks the cases too. Cases must be same otherwise it returns false while comparing two strings.
See the images for the output.
•••♪
Answer:
Program:-
import java.util.*;
public class Main
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
String st,st1;
System.out.println("Enter the 1st String");
st=in.next();
System.out.println("Enter the 2nd String");
st1=in.next();
if(st.equalsIgnoreCase(st1))
{
System.out.println("Both String are equal");
}
else
{
System.out.println("Both String are unequal");
}
}
}
Output is attached
- The method equalsIgnoreCase is used to compare 2 strings without checking the case of the Strings.