I am to create a method that checks spelling.
The user is asked to enter a word, which is stored in str
. And the word from the dictionary is stored in word
, however I am having issues. I didn't manage to get this code working.
The method should return true
in the case if it is possible to remove one of the letters from str
(the string entered by the user) so that it becomes the same as the word from the dictionary.
For example, if the user enters the string “appple”
the method will return true
for the word “apple”
.
//You neeed to complete this method. The method should return
//true, if a letter is deleted from any place of str (the user entered)
//it becomes the word (a word from the dictionary).
public static boolean oneAdditionalLetter(String word, String str) {
if (str.length() <= word.length() && str.length() == word.length() 2)
return false;
char temp1;
char temp3;
for(int i = 0; i < str.length() - 1; i ){
temp1 = str.charAt(i);
temp3 = word.charAt(i);
if(temp1 == temp3)
return true;
}
return false;
}
CodePudding user response:
As I understand your task, you need to find out whether two strings differ in length by one have all the same letters except from only one.
For that, first you need to check if lengths of string meat the requirement and then validate the given string by iterating over its indices and checking its characters against the character of the string from the dictionary.
Your condition that is meant to validate the length is incorrect.
str.length() <= word.length() && str.length() == word.length() 2
Length of the given string is a distinct value, and it could be either less or equal to the word.length()
or equals to word.length() 2
. But not both, hence this condition is always false
.
To fix it logical and &&
needs to be replaced with logical or ||
, and in the second part instead ==
you have to apply >=
to caver the whole range of possible invalid sizes. And it can be simplified to
str.length() != word.length() 1
In order to validate the contents of the given string, you need to introduce a boolean
flag, indicating that a letter in which two strings differ was found.
If the flag is set to true
(i.e. mismatch was found) you need to apply a shift by adding 1
to the index of the given string while checking equality of characters of the two strings, because a single non-matching character shouldn't be taken into account. And if the second mismatch is being encountered, the given string is considered to be invalid - method immediately returns false
.
Only when all characters of the given string are successfully checked against the characters of the string from the dictionary, it's proved to be valid (containing only one non-matching character). Hence, return
statement after the loop yields true
.
public static boolean hasOneAdditionalLetter(String word, String str) {
if (str.length() != word.length() 1) {
return false;
}
boolean mismatchFound = false;
for (int i = 0; i < str.length() - 1; i ) {
if (!mismatchFound) {
if (str.charAt(i) != word.charAt(i)) {
mismatchFound = true; // first mismatch was found
}
} else if (str.charAt(i 1) != word.charAt(i)) { // because mismatch was already found shift i 1 is used
return false; // there's more than mismatch
}
}
return true; // all letter apart from one are the same
}
public static void main(String[] args) {
System.out.println(hasOneAdditionalLetter("apple", "appple")); // true
System.out.println(hasOneAdditionalLetter("horse", "hhorse")); // true
System.out.println(hasOneAdditionalLetter("mountain", "mounYtain")); // true
System.out.println(hasOneAdditionalLetter("fiz", "baz")); // false
}
Output
true - "apple", "appple"
true - "horse", "hhorse"
true - "mountain", "mounYtain"
false - "fiz", "baz"
Side-note: method names usually contain a verb because they are meant to represent actions (get
, put
, compute
, remove
, etc.), methods return a boolean
value traditionally start with is
or has
.
CodePudding user response:
The reason you get true
is the return in the if-statement it simply ends the function and returns true
at the first test case that is true.
For example, if the user entered string is “appple” the method will return true for the word “apple” the function will return after the "a" is checked.
to make it work modify the if-statement to return false if it finds an unmatched letter and the function itself to return true at the end.