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...
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...
The use of list or any other data type not allowed only string and string methods...
The use of list or any other data type not allowed only string and string methods .Hint: Depending on how you plan to solve this problem, accumulator variable initialized as an empty string may help in this question. Write a function called weaveop, that takes a single string parameter (s) and returns a string. This function considers every pair of consecutive characters in s. It returns a string with the letters o and p inserted between every pair of consecutive...
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.
8.25 Lab 6b Switching Up Months Objectives Use Boolean expressions involving String comparison Use String methods...
8.25 Lab 6b Switching Up Months Objectives Use Boolean expressions involving String comparison Use String methods substring, toUpperCase, equals, and charAt Use switch program control statement Print out a program and turn it in for "eyes-on" grading Introduction In this lab you will continue using the other type of conditional statements: the switch-case. For switch-case conditional statements, the idea is that you have a variable (the switch) that will determine the next course of action depending on its value (the...
Write a JAVA code using regex methods and concepts The test string S should match the...
Write a JAVA code using regex methods and concepts The test string S should match the following requirements: S must be of length 6 First character should not be a digit (1, 2, 3, 4, 5, 6, 7, 8, 9 or 0). Second character should not be a lowercase vowel (a, e, i, o or u). Third character should not be b, c, D or E. Fourth character should not be a whitespace character (\r, \n, \t, \f or the...
Upon completion of this exercise, you will have demonstrated the ability to: Use C# loops Construct...
Upon completion of this exercise, you will have demonstrated the ability to: Use C# loops Construct conditions INTRODUCTION Your instructor will assign one of the following projects, each involves the user of loops and the construction of conditions to control them. CODING STANDARDS The following coding standards must be followed when developing your program: An opening documentation at the beginning of the source file describing the purpose, input, process, output, author, last modified date of the program. Write only one...
OBJECTIVE Upon completion of this exercise, you will have demonstrated the ability to: Use C# loops...
OBJECTIVE Upon completion of this exercise, you will have demonstrated the ability to: Use C# loops Construct conditions INTRODUCTION Your instructor will assign one of the following projects, each involves the user of loops and the construction of conditions to control them. CODING STANDARDS The following coding standards must be followed when developing your program: An opening documentation at the beginning of the source file describing the purpose, input, process, output, author, last modified date of the program. Write only...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT