0

The task is to create a function that takes two strings as arguments and returns true if the second string is a suffix of the first. My code worked for all test cases except two. I can't figure out where I'm going wrong.

Examples:

isSuffix("vocation", "-logy") ➞ false

isSuffix("arachnophobia", "-phobia") ➞ true

My code:

    public static boolean isSuffix(String word, String suffix) {
    String suff = "";
    for(int i = 1; i < suffix.length(); i++)    suff += "" + suffix.charAt(i);
    
    for(int i = 0; i < suff.length(); i++){
        if(!(word.charAt(suff.length()-1-i) == suff.charAt(suff.length()-1-i))) return false;
    }
    
    return true;        
}

The test cases I'm failing:

@Test
public void test5() {
    assertThat(Program.isSuffix("arachnophobia", "-phobia"), is(true));
}

@Test
public void test6() {
    assertThat(Program.isSuffix("rhinoplasty", "-plasty"), is(true));
}
Community
  • 1
  • 1
coderfromhell
  • 435
  • 1
  • 4
  • 13

4 Answers4

1

First, to remove the hypen, just use substring, doing string concatenation in loop is not a good idea , giving String suff = suffix.substring(1)

Then, you're using stuff.length instead of word.length(), giving in fact

public static boolean isSuffix(String word, String suffix) {
    String suff = suffix.substring(1);
    for (int i = 0; i < suff.length(); i++) {
        if (!(word.charAt(word.length() - 1 - i) == suff.charAt(suff.length() - 1 - i))) {
            return false;
        }
    }
    return true;
}

And in fact you could just substring from word with the same length of the suffix and check if they are equals

public static boolean isSuffix(String word, String suffix) {
    String suff = suffix.substring(1);
    String wordSuffix = word.substring(word.length() - suff.length());
    return suff.equals(wordSuffix);
}

And even better using .endsWith, but this is assuming you can use the builtin methods

public static boolean isSuffix(String word, String suffix) {
    return word.endsWith(suffix.substring(1));
}

Also this code assumes that both are non null, and there always is the hyphen

azro
  • 53,056
  • 7
  • 34
  • 70
  • Thank you, but may I ask why doing string concatenation in a loop is not a good idea? – coderfromhell Apr 30 '20 at 17:10
  • 1
    @SthitaprajnaMishra It creates a new string each time, first a StringBuilder would be better https://stackoverflow.com/questions/7817951/string-concatenation-in-java-when-to-use-stringbuilder-and-concat if really needed – azro Apr 30 '20 at 17:11
1

You can use String.endsWith() and String.substring() to solve the problem easily, with minimal code (after included validation of course):

public static boolean isSuffix(String word, String suffix) {
    if(word == null || suffix == null) return false;
    if(suffix.length() <= 1 || !suffix.startsWith("-")) return false;

    return word.endsWith(suffix.substring(1));
}
pczeus
  • 7,709
  • 4
  • 36
  • 51
1

Your function is rather complicated, as you loop through the letters.

public static boolean isSuffix(String word, String suffix) {
    // Remove first character
    String suff = suffix.substring(1);
    // Get Suffix from word
    String wordEnding = word.substring(word.length()-suffix.legnth());

    // Compare
    if(wordEnding.compareTo(suff)==0){
      return true;
    }
    return false;
}
chenino
  • 67
  • 5
0

word.charAt(suff.length()-1-i) should be word.charAt(word.length()-1-i)

I just realized this.

coderfromhell
  • 435
  • 1
  • 4
  • 13