Fill the below code to compare two strings and print "yes" if they are same else print "no".Don't consider the case. Example, if the strings are "Good" and "good" the output should be "yes".Code : Scanner sc=new Scanner(System.in); String s1=sc.next(); String s2=sc.next(); if() System.out.println("yes"); else System.out.println("no");
Question
Fill the below code to compare two strings and print "yes" if they are same else print "no".Don't consider the case. Example, if the strings are "Good" and "good" the output should be "yes".Code : Scanner sc=new Scanner(System.in); String s1=sc.next(); String s2=sc.next(); if() System.out.println("yes"); else System.out.println("no");
Solution
Here is the completed code:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s1 = sc.next();
String s2 = sc.next();
if(s1.equalsIgnoreCase(s2)) {
System.out.println("yes");
} else {
System.out.println("no");
}
}
}
In the if condition, I used the equalsIgnoreCase method which compares two strings irrespective of the case. If both strings are equal, it returns true, otherwise it returns false.
Similar Questions
Develop a Java program with a method for comparing two strings.
The program needs to compare two given strings lexicographically and output the difference between them. (using compareTo() function)If the first string is greater than the second string, it should output a positive value, If they are equal it should output 0If the first string is less than the second string, it should output a negative valueInput format :The first line of the input consists of a string.The second line of the input consists of a string.Output format :The output should display the following constraintsNote :If (string1 > string2), it returns a positive value (the difference between the characters).If both the strings are equal lexicographically, i.e., (string1 == string2), it returns 0.If (string1 < string2), it returns a negative value (the difference between the characters).Sample test cases :Input 1 :harryharryOutput 1 :0Input 2 :helloworldOutput 2 :-15Input 3 :tiger lionOutput 3 :8
Which of the following will loop until the user enters a value for String for str2 that does NOT match str1? (Mark ALL that apply)Scanner input = new Scanner(System.in);String str1 = input.nextLine();String str2 = input.nextLine();while ( CONDITION ) { str2 = input.nextLine();}Group of answer choicesstr1.equals(str2)str2.equals(str1)str2 == str1str1 == str2
Write a C program to compare two strings character by character.Read the string using character pointer.Pass the character pointer to a function "compare" and print equal/not equal.Sample InputcatCatSample Outputnot equal
public class HelloWorld { public static void main(String[] args) { String s1 = "Coding"; String s2 = "Coding"; String s3 = new String("Coding"); System.out.print(s1 == s2); System.out.print(" "); System.out.print(s1 == s3); }}Options: Pick one correct answer from belowtrue falsetrue truefalse falsefalse true
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.