Question

In: Computer Science

This exercise will test your ability to use the basic instance methods of String. The exercise...

This exercise will test your ability to use the basic instance methods of String. The exercise is to create a program that compares two Strings ignoring the case and returns the offset between the first two distinct characters in the two Strings from left to right.

All the input strings have five characters.

For this exercise, compareToIgnoreCase() and compareTo() methods are not allowed.

Example 1, "London" and "Lately" will return the offset between 'o' and 'a', which is 14

Example 2, "London" and "lately" will return the offset between 'o' and 'a', which is 14

Example 3, "LONDON" and "lately" will return the offset between 'o' and 'a', which is 14

Example 4, "LONDON" and "Loathe" will return the offset between 'n' and 'a', which is 13

Example 4, "LONDON" and "london" will return 0

The class name should be: Lab14_CustomizedStringComparison

Solutions

Expert Solution

import java.io.*; 
import java.lang.*;
import java.util.*;
public class Main
{
        public static void main(String[] args) {
            Scanner sc= new Scanner(System.in); 
            System.out.print("Enter string1: ");  
        String str1= sc.next();
        System.out.print("Enter string2: ");  
        String str2= sc.next();
        str1=str1.toLowerCase();
        str2=str2.toLowerCase();
        int ans;
        ans=compare(str1,str2);
                System.out.println("Offset: "+ans);
        }
        public static int compare(String str1,String str2)
        {
            int ans=0;
        for(int i=0,j=0;i<str1.length()&&j<str2.length();i++,j++)
        {
            int t1= (int)str1.charAt(i);
            int t2=(int)str2.charAt(j);
            if (t1-t2 !=0){
                ans=Math.abs(t1-t2);
                break;
            }
                
        }
        return ans;
        }
}

Related Solutions

A researcher wishes to test the effects of excerise on the ability to complete a basic...
A researcher wishes to test the effects of excerise on the ability to complete a basic skills test. He designs a pre-test and post-test to give to each participant. You believe that there was an increase in the scores. You believe the population of the differences is normally distributed, but you do not know the standard deviation. When calculating difference use Post-test minus Pre-test. pre-test post-test 52 60 60 55 44 49 92 94 84 76 55 65 64 58...
c++ The purpose of this project is to test your ability to use files, class design,...
c++ The purpose of this project is to test your ability to use files, class design, operator overloading, and Strings or strings effectively in program design Create a program which will read a phrase from the user and create a framed version of it for printing. For example, the phrase "hello world"would result in: ********* * hello * * world * ********* Whereas the phrase "the sky is falling"might be: *********** * the * * sky * * is *...
The National Assessment of Educational Progress (NAEP) gave a test of basic arithmetic and the ability...
The National Assessment of Educational Progress (NAEP) gave a test of basic arithmetic and the ability to apply it in everyday life to a sample of 840 men 21 to 25 years of age. Scores range from 0 to 500; for example, someone with a score of 325 can determine the price of a meal from a menu. The mean score for these 840 young men was x⎯⎯⎯x¯ = 272. We want to estimate the mean score μμ in the...
Discuss your ability to use specialized audit tools including sampling methods. Can you be an effective...
Discuss your ability to use specialized audit tools including sampling methods. Can you be an effective auditor without using these tools when auditing transaction cycles such as the Revenue Cycle? Explain.
1. Describe the difference between instance methods and class methods in Java and give an example...
1. Describe the difference between instance methods and class methods in Java and give an example of each. 2. A class variable is visible to and shared by all instances of a class. How would such a variable be used in an application? 3. Describe the difference between abstract classes and concrete classes, giving an example of each. 4. Explain how data are encapsulated and information is hidden in Java? 5. Explain the difference between a class and an interface...
Are there any music and movement activities that you use in your own classroom; for instance,...
Are there any music and movement activities that you use in your own classroom; for instance, The Hokey Pokey?
8.66 Significance Test for Exergaming in Canada. Refer to exercise 8.64. Use a significance test to...
8.66 Significance Test for Exergaming in Canada. Refer to exercise 8.64. Use a significance test to compare the proportions. Write a short statement interpreting this result. 8.64  Exergaming in Canada. Exergames are active video games such as rhythmic dancing games, virtual bicycles, balance board simulators, and virtual sports simulators that require a screen and a console. A study of exergaming practiced by students from grades 10 and 11 in Montreal, Canada, examined many factors related to participation in exergaming.22 Of...
Nonparametric Methods In this assignment, we will use the following nonparametric methods: The Wilcoxon signed-rank test:...
Nonparametric Methods In this assignment, we will use the following nonparametric methods: The Wilcoxon signed-rank test: The Wilcoxon signed-rank test is the nonparametric test analog of the paired t-test. The Wilcoxon rank-sum test or the Mann-Whitney U test: The Wilcoxon rank-sum test is an analog to the two-sample t-test for independent samples. Part 1: Wilcoxon Signed-Rank Test Let's take a hypothetical situation. The World Health Organization (WHO) wants to investigate whether building irrigation systems in an African region helped reduce...
RUBY Write a DNA class that gets initialized with a string of nucleotide characters. An instance...
RUBY Write a DNA class that gets initialized with a string of nucleotide characters. An instance of DNA responds to the to_s, length, hamming_distance, contains, positions, and frequencies messages. Where a and b are DNA instances of the same length, the message send a.hamming_distance(b) returns the Hamming distance between a and b. a.contains(b) returns true if b appears as a substring in a, and returns false otherwise. a.positions(n) returns an array of the (zero-based) positions in which the nucleotide n...
using 0.01 level of significance use the information given to test whether a person's ability in...
using 0.01 level of significance use the information given to test whether a person's ability in math is independent of his or her interest in calculus. Ability in math Low Average High   Low 63 42 15 Average 58 61 31 High 14 47 29 Interest in calculus - left
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT