Question

IN JAVA!!!! Write a method that returns the longest common prefix between a phrase A and...

IN JAVA!!!!

Write a method that returns the longest common prefix between a phrase A and a phrase B. If the two phrases do not share a common prefix, return the empty string “no prefix”. Your method should either take two strings or two char arrays as arguments (A, B) and return a string.

Test your method in the main with these three pairings.

Example

A: snowball

B: snowcone

Return: “snow”

A: river

B: rover

Return: “r”

A: monday

B: tuesday

Return: “no prefix”

0 0
Add a comment Improve this question Transcribed image text
Answer #1
public class CommonPrefix {

    public static String commonPrefix(String a, String b) {
        String common = "";
        int len = Math.min(a.length(), b.length());
        for(int i = 0; i < len; ++i) {
            if(a.charAt(i) != b.charAt(i)) {
                break;
            }
            common += a.charAt(i);
        }
        if(common.isEmpty()) {
            return "no prefix";
        } else {
            return common;
        }
    }

    public static void main(String[] args) {
        System.out.println(commonPrefix("snowball", "snowcone"));
        System.out.println(commonPrefix("river", "rover"));
        System.out.println(commonPrefix("monday", "tuesday"));
    }

}

no prefix Process finished with e xit code 0

Add a comment
Know the answer?
Add Answer to:
IN JAVA!!!! Write a method that returns the longest common prefix between a phrase A and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using the programming language Java: Write a function to find the longest common prefix string amongst...

    Using the programming language Java: Write a function to find the longest common prefix string amongst an array of strings.

  • 3. (4 pt) Write a Python program to find the longest common prefix string amongst an...

    3. (4 pt) Write a Python program to find the longest common prefix string amongst an array of strings. You are required to write a function max- Prefix, which when called by the main program would get the array of strings as input and return a single string that would either be the longest 1 common prefix or a string ”NULL” depending on the input. (Note: The array is input from the keyboard in the main program (930)) If there...

  • in java!! Write a method to check prefix of two strings that have more than 3...

    in java!! Write a method to check prefix of two strings that have more than 3 characters. A 3-char prefix of a word “class” will be “cla”. The method should take two strings as parameters, then compare 3-char prefix of two strings. Write a method to check suffix of two strings that have more than 3 characters. A 3-char suffix of a word “world” will be “rld”. The method should take two strings as parameters, then compare 3-char suffix of...

  • (9) Complete the below Java method recursively, which takes in a String and returns if it's...

    (9) Complete the below Java method recursively, which takes in a String and returns if it's a palindrome. Remember a palindrome means a string that is the same reversed, like "deed", "racecar" or "tacocat". The empty string "" is also a palindrome. public boolean isPalindrome (String word) { (8) (a) (8 points) Implement merging two sorted arrays into one sorted ar- ray. These arrays are sorted in descending order. For example, (5, 4, 2). Return an array with all the...

  • How to write a method in Java with these set of instructions: Method name will be:...

    How to write a method in Java with these set of instructions: Method name will be: public static boolean containsNumber(String[] array, String str) { // instruction: returns true if str is an element that is equal to an element of array // return false if str does not appear in array. // using compare String equality (str1.equals(str2). // JUST assume that array is not null and not empty // and NONE of strings in array is null. Assume str is...

  • In C++ write a program that will read three strings from the user, a phrase, a...

    In C++ write a program that will read three strings from the user, a phrase, a letter to replace, and a replacement letter. The program will change the phrase by replacing each occurrence of a letter with a replacement letter. For example, if the phrase is Mississippi and the letter to replace is 's' the new phrase will be "miizzizzippi". in the function, replace the first parameter is a modifiable string, the second parameter is the occurrence of a letter...

  • In Java: Let's create a method that has parameters. Write a method, called returnHours that returns...

    In Java: Let's create a method that has parameters. Write a method, called returnHours that returns a string. Make the string from a string representing your name and a number representing the hours you spend sleeping, both are values that you pass in from the main. Create the variables to pass into the method call in main. Call the method and print the return value in main. Run and test it.

  • Programming project in Java: You are allowed to use the following methods from the Java API:...

    Programming project in Java: You are allowed to use the following methods from the Java API: class String length charAt class StringBuilder length charAt append toString class Character any method Create a class called HW2 that contains the following methods: 1. isAlphabeticalOrder takes a String as input and returns a boolean: The method returns true if all the letters of the input string are in alphabetical order, regardless of case. The method returns false otherwise. Do not use arrays to...

  • Java programming: I need to create a method that is given a 2D char array and...

    Java programming: I need to create a method that is given a 2D char array and a String that returns a part of the original 2D array. The input string will tell the method which rows from the input array need to be returned. For example, a 5x5 char array input along with a string "0 4" would return rows 1 and 5 of the input array. The String input will always be numbers divided by spaces and will not...

  • in Java please (a) Write a static method abbreviate( ) which is passed a String s...

    in Java please (a) Write a static method abbreviate( ) which is passed a String s and an int max. The method returns a new String which contains the content of s abbreviated to at most max characters, but where the last three characters are ellipses (i.e., the characters . . .). For example, if s is "Too bad Spongebob’s not here to enjoy Spongebob not being here. ---Squidward." and max is 10, the method returns the 10-character String "Too...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT