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

(java) Write a program that uses String method regionMatches to compare two stri

ID: 3839921 • Letter: #

Question

(java) Write a program that uses String method regionMatches to compare two strings input by the user. The program should prompt the user to enter two strings, the starting index in the first string, the starting index in the second string, and the number of characters to be compared. The program should print whether or not the strings are equal. (Ignore the case of the characters during comparison)

Run like:

Enter first string:Have yourself a mery little Christmas.

Enter second string:It's beginning to look a lot like christmas.

Enter starting index for first string:29

Enter starting index for second string:34

Enter number of characters to be compared:9

true

Explanation / Answer

answer:

import java.io.*;

import java.lang.string.*;

class stringcomp {

            string string1, string2;

            int no_of_chars, start_index1, start_index2;

            boolean result;

            bufferedreader br = new bufferedreader(new inputstreamreader(system.in));

            void comparestrings() {

                        try {

                                    system.out.println("enter string 1 ");

                                    string1 = br.readline();

                                    system.out.println("enter string 2 ");

                                    string2 = br.readline();

                                    system.out.println("enter the no.of characters to compare ");

                                    no_of_chars = integer.parseint(br.readline());

                                    system.out.println("enter the starting index of string1 ");

                                    start_index1 = integer.parseint(br.readline());

                                    system.out.println("enter the starting index of string2 ");

                                    start_index2 = integer.parseint(br.readline());

                                    result = string1.regionmatches(true, start_index1, string2,

                                                            start_index2, no_of_chars);

                                    if (result)

                                                system.out.println("the region matches ");

                                    else

                                                system.out.println("the region doesn't match ");

                                    if (string1.equalsignorecase(string2))

                                                system.out.println("both the strings are same");

                                    else if (string1.comparetoignorecase(string2) > 0)

                                                system.out.println("string1 is greater than string2");

                                    else if (string1.comparetoignorecase(string2) < 0)

                                                system.out.println("string1 is less than string2");

                        } catch (exception e) {

                        }

            }

}

class stringmatch {

            public static void main(string[] args) {

                        stringcomp strcmp = new stringcomp();

                        strcmp.comparestrings();

            }

}