id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

 

srp4379@lausd.net 3-stringbasics > almostEndsFUL
prev  |  next  |  chance

public boolean almostEndsFUL(String str)

Given a String, return true if it ALMOST ends with the suffix "ful".
Return false if it doesn't.
What does ALMOST mean exactly?
In this case, if you were to chop off the last character,
return true if that truncated substring ends with "ful".

Explanation: We've already mentioned that endsWith() does NOT have two versions.
That is, there is no version with a 2nd parameter, as with startsWith().
That means that any string you test will only return true if it
actually ENDS WITH the string you are testing for.

That being said, there is a WORKAROUND if you want to test whether
a string "ends with" a letter sequence one or more characters BEFORE
the end of the string. That is, by ignoring one or more characters at the end.
This can be done using startsWith()!.

Begin by writing a method to test whether a string ends with "ful".

public boolean almostEndsFUL(String str) {
  boolean ends = false;
  int len = str.length();
  if (str.startsWith("ful",len-3)) {
    ends = true;
  }
  return ends;
}
Change "len-3" to "len-4" !
This will direct startsWith() to begin checking, not at the 3rd-to-last letter,
but beginning at the 4th-to-last letter from the end.
public boolean almostEndsFUL(String str) {
  boolean ends = false;
  int len = str.length();
  if (str.startsWith("ful",len-4)) {
    ends = true;
  }
  return ends;
}

almostEndsFUL("mindful") → false
almostEndsFUL("mindfulZ") → true
almostEndsFUL("handful") → false

...Save, Compile, Run (ctrl-enter)

public boolean almostEndsFUL(String str) { }

Editor font size %:
Shorter output


Forget It! -- delete my code for this problem

Progress graphs:
 Your progress graph for this problem
 Random user progress graph for this problem
 Random Epic Progress Graph

Java Help

Misc Code Practice

Difficulty: 1

Copyright Nick Parlante 2017 - privacy