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

Write a method named sameDashes that takes two strings as parameters and that re

ID: 3529475 • Letter: W

Question

Write a method named 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 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. string1: "hi--there-you." "-15-389" "criminal-plan" "abc" string2: "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")

Explanation / Answer

please rate - thanks


import java.util.*;

public class main

{public static void main(String[] args)

{System.out.println(sameDashes("1st-has-more characters", "2nd-has-less"));

System.out.println( sameDashes("1st-has-less", "2nd-has-more characters"));

System.out.println(sameDashes("1st-has-more-characters", "2nd-has-less"));

System.out.println( sameDashes("1st-has-less", "2nd-has-more-characters"));

}

public static boolean sameDashes(String a, String b)

{int i;

int lenb=b.length();

int lena=a.length();

for(i=0;i<lena;i++) //check each character of 1st string

if(a.charAt(i)=='-') //if find a dash

if(i<lenb) //is length within 2nd string

{if(b.charAt(i)!='-') //if it is check 2nd string, and if not

return false; //return false

}

else //first longer and - found beyond size of

return false;

//get here if all match

//so now check if longer has a -

if(lena>=lenb) //first is longer or = so all match

return true;

//check rest of 2nd

for(i=lena;i<lenb;i++)

if(b.charAt(i)=='-') //if find an extra

return false; //not the same

return true;

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote