Knowee
Questions
Features
Study Tools

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

Question

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

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is a step-by-step guide on how to solve this problem:

Step 1: First, you need to import the Scanner class from the java.util package. This class is used to get the user input.

import java.util.Scanner;

Step 2: Create a new Scanner object to read the input from the user.

Scanner sc = new Scanner(System.in);

Step 3: Read the first string from the user.

String str1 = sc.nextLine();

Step 4: Read the second string from the user.

String str2 = sc.nextLine();

Step 5: Use the compareTo() method to compare the two strings lexicographically. This method returns an integer value which is the difference between the two strings.

int result = str1.compareTo(str2);

Step 6: Print the result.

System.out.println(result);

Here is the complete code

This problem has been solved

Similar Questions

What is the function to compare two strings lexicographically in C programming?

Develop a Java program with a method for comparing two strings.

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

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");

Which function is used to compare two strings lexicographically in C++?compare()equals()strcmp()strcomp()

1/3

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.