This exercise is to test your understanding of Java Strings. A sample String declaration:String myString = "Hello World!"The elements of a String are called characters. The number of characters in a String is called the length, and it can be retrieved with the String.length() method.Given two strings of lowercase English letters, and , perform the following operations:Sum the lengths of and .Determine if is lexicographically larger than (i.e.: does come before in the dictionary?).Capitalize the first letter in and and print them on a single line, separated by a space.Input FormatThe first line contains a string . The second line contains another string . The strings are comprised of only lowercase English letters.Output FormatThere are three lines of output:For the first line, sum the lengths of and .For the second line, write Yes if is lexicographically greater than otherwise print No instead.For the third line, capitalize the first letter in both and and print them on a single line, separated by a space.Sample Input 0hellojavaSample Output 09NoHello JavaExplanation 0String is "hello" and is "java". has a length of , and has a length of ; the sum of their lengths is .When sorted alphabetically/lexicographically, "hello" precedes "java"; therefore, is not greater than and the answer is No.When you capitalize the first letter of both and and then print them separated by a space, you get "Hello Java".
Question
This exercise is to test your understanding of Java Strings. A sample String declaration:String myString = "Hello World!"The elements of a String are called characters. The number of characters in a String is called the length, and it can be retrieved with the String.length() method.Given two strings of lowercase English letters, and , perform the following operations:Sum the lengths of and .Determine if is lexicographically larger than (i.e.: does come before in the dictionary?).Capitalize the first letter in and and print them on a single line, separated by a space.Input FormatThe first line contains a string . The second line contains another string . The strings are comprised of only lowercase English letters.Output FormatThere are three lines of output:For the first line, sum the lengths of and .For the second line, write Yes if is lexicographically greater than otherwise print No instead.For the third line, capitalize the first letter in both and and print them on a single line, separated by a space.Sample Input 0hellojavaSample Output 09NoHello JavaExplanation 0String is "hello" and is "java". has a length of , and has a length of ; the sum of their lengths is .When sorted alphabetically/lexicographically, "hello" precedes "java"; therefore, is not greater than and the answer is No.When you capitalize the first letter of both and and then print them separated by a space, you get "Hello Java".
Solution
Here is a step-by-step solution in Java:
- First, we need to read the input strings. We can use the Scanner class in Java to do this.
Scanner sc=new Scanner(System.in);
String A=sc.next();
String B=sc.next();
- Next, we need to find the sum of the lengths of the two strings. We can use the length() method of the String class to do this.
int sum = A.length() + B.length();
System.out.println(sum);
- To determine if string A is lexicographically larger than string B, we can use the compareTo() method of the String class. If A is lexicographically larger than B, compareTo() will return a positive number.
System.out.println(A.compareTo(B) > 0 ? "Yes" : "No");
- Finally, to capitalize the first letter of both strings and print them on a single line, separated by a space, we can use the substring() method of the String class to separate the first character from the rest of the string, and then use the toUpperCase() method to capitalize the first character.
System.out.println(A.substring(0, 1).toUpperCase() + A.substring(1) + " " + B.substring(0, 1).toUpperCase() + B.substring(1));
So, the complete code will look like this:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String A=sc.next();
String B=sc.next();
int sum = A.length() + B.length();
System.out.println(sum);
System.out.println(A.compareTo(B) > 0 ? "Yes" : "No");
System.out.println(A.substring(0, 1).toUpperCase() + A.substring(1) + " " + B.substring(0, 1).toUpperCase() + B.substring(1));
}
}
Similar Questions
Write a java program to accept a String and the perform the following operations:a) Count the number of words with the first and the last letter being same.
Develop a Java program with a method for comparing two strings.
Tom is playing scrabble and forms new words. He wants to count the number of characters present in the word formed by him. Help him to do this using the below java program.Code :import java.util.Scanner;public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String str=sc.next(); //find length and print the same int len= ; System.out.println("The length of the word is "+) ; }}
What will be the output of the program?String x = "xyz";x.toUpperCase(); /* Line 2 */String y = x.replace('Y', 'y');y = y + "abc";System.out.println(y);abcXyZabcxyzxyzabcXyZabc
How to find all occurrences of a character or substring in Strings in Java?
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.