I have to write a program in Java that compares two strings for dashes. The test only returns true if each string has the same amount of dashes in the same position in the strings.
Example:
Comparing the following two strings
String string1 = “two-twenty-nine”
String string2 = “ten-fourty-five and I’m hungry.”
with the above criteria would return true. It doesn’t matter whether one string is longer than the other.
Any assistance would be appreciated!
I have tried:
– converting the strings to char arrays and then comparing the indexes
– Using String.indexOf() for each string, and then creating a variable int newStart = String.indexOf() with the index of the previous dash as the new starting point to look from
` public static void sameDashes(String string1, String string2) {
int count = 0;
char index1 = ' ';
char index2 = ' ';
char dash = '-';
char[] string1Array = string1.toCharArray();
char[] string2Array = string2.toCharArray();
while (count < string1Array.length && count < string2Array.length) {
if (string1Array[index1] == dash && string2Array[index2] == dash) {
System.out.println("true");
}
}
}
Since I suspect this is homework, I’m just going to outline a solution.
indexOfmethod, and get aListofIntegers representing the positions of the dashes.Lists, and see if they have the samesize().