logo Practice-It logo

sameDashes

Language/Type: Java method basics return Strings
Author: Eric Spishak (on 2010/12/28)

Write a method sameDashes that takes two Strings as parameters and that returns whether or not they have dashes in the same places (returning true if they do and returning false otherwise). For example, below are four pairs of Strings of equal length that have the same pattern of dashes. Notice that the last pair has no dashes at all.

string 1:    "hi--there-you."    "-15-389"    "criminal-plan"    "abc"
string 2:    "12--(134)-7539"    "-xy-zzy"    "(206)555-1384"    "9.8"

To be considered a match, the Strings must have exactly the same number of dashes in exactly the same positions. The Strings might be of different length. For example, the following calls should each return true:

sameDashes("1st-has-more characters", "2nd-has-less")
sameDashes("1st-has-less", "2nd-has-more characters")

because the Strings each have two dashes and they are in the same positions. But the following calls should each return false because the longer string has a third dash where the shorter string does not:

sameDashes("1st-has-more-characters", "2nd-has-less")
sameDashes("1st-has-less", "2nd-has-more-characters")
Type your solution here:


This is a method problem. Write a Java method as described. Do not write a complete program or class; just the method(s) above.

You must log in before you can solve this problem.


Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.