Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program which reads two Strings of any length from the console and displ

ID: 3802497 • Letter: W

Question

Write a program which reads two Strings of any length from the console and displays the following information about the strings: bullet Display the number of characters in each String. bullet Display whether the first string is a substring of the second. If the first string is a substring of the second, display where the string appears (in the beginning, middle, or end). bullet Display whether the second string is a substring of the first. If the second string is a substring of the first, display where the string appears (in the beginning, middle, or end). bullet Display if the two strings are equal or not. If the two strings are not equal, say whether or not the first string is alphabetically less than or greater than the second string. Input Validation: bullet No validation required. Requirements: bullet All comparisons must ignore the case of the string. Sample Output 1: Enter the first string: abcde Enter the second string: abcde "abcde" has 5 characters. "abcde" has 5 characters. "abcde" is a substring of "abcde" and appears at the beginning, middle, and end of the string. "abcde" is a substring of "abcde" and appears at the beginning, middle, and end of the string. "abcde" and "abcde" are equal

Explanation / Answer

TwoStringsTest.java

import java.util.Scanner;


public class TwoStringsTest {

   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       System.out.print("Enter the first string: ");
       String first = scan.nextLine();
       System.out.print("Enter the second string: ");
       String second = scan.nextLine();
       int firstStringLength = first.length();
       int secondStringLength = second.length();
       System.out.println();
       System.out.println("""+first+"" has "+firstStringLength+" characters");
       System.out.println("""+second+"" has "+secondStringLength+" characters");
       int firstStringIndex = second.toLowerCase().indexOf(first.toLowerCase());
       int secondStringIndex = first.toLowerCase().indexOf(second.toLowerCase());
       String firstString = """+first+"" is a substring of ""+second+"" and appears at the ";
       String secondString = """+second+"" is a substring of ""+first+"" and appears at the ";
       if(firstStringIndex != -1){
          
           if(firstStringIndex == 0 ){
               firstString = firstString+" begining, ";
           }
           if((firstStringIndex != 0 && secondStringLength != firstStringIndex + firstStringLength) || firstStringLength==secondStringLength){
               firstString = firstString+" middle, ";
           }
           if(secondStringLength == firstStringIndex + firstStringLength ){
               firstString = firstString+" end ";
           }
           firstString = firstString +" of the string.";
           System.out.println(firstString);
       }
       else{
           System.out.println("""+first+"" is not a substring of ""+second+""");
       }
       if(secondStringIndex != -1){
           if(secondStringIndex == 0 ){
               secondString = secondString+" begining, ";
           }
           if((secondStringIndex != 0 && firstStringLength != secondStringIndex + secondStringLength) || firstStringLength==secondStringLength){
               secondString = secondString+" middle, ";
           }
           if(firstStringLength == secondStringIndex + secondStringLength ){
               secondString = secondString+" end ";
           }
           secondString = secondString +" of the string.";
           System.out.println(secondString);
       }

       else{
           System.out.println("""+second+"" is not a substring of ""+first+""");
       }
       if(first.equalsIgnoreCase(second)) {
       System.out.println("""+first+"" and ""+second+"" are equal");
       }
       else {
           System.out.println("""+first+"" and ""+second+"" are not equal");  
       }

   }

}

Output:

Enter the first string: abcde
Enter the second string: abcde

"abcde" has 5 characters
"abcde" has 5 characters
"abcde" is a substring of "abcde" and appears at the begining, middle, end of the string.
"abcde" is a substring of "abcde" and appears at the begining, middle, end of the string.
"abcde" and "abcde" are equal

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote