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

 

endsGerund


public boolean endsGerund(String str)

Given a String, return true if it's a gerund, i.e. it ends with "ing". Return false if it doesn't.
The method is case-sensitive. Capitals matter DON'T match their lowercase counterparts, i.e. "Abc" does not match "aBC".

Explanation:
Begin completing this method as usual:

public boolean endsGerund(String str) {
  boolean ends= false;
  if (_____) {
    ends = true;
  }
  return ends;
}
Use the String method endsWith() inside the parentheses:
public boolean endsGerund(String str) {
  boolean ends = false;
  if ( str.endsWith("ing") ) {
    ends = true;
  }
  return ends;
}
Note that - unlike startsWith() - there are NOT two versions of endsWith().
That is, there is no version with a 2nd parameter.
That means that any string you test will only return true if it
actually ENDS WITH the string you are testing for.
______________________________________

NOTE: There is actually a way to solve this problem using startsWith().
public boolean endsGerund(String str) {
  boolean ends = false;
  int len = str.length();
  if ( str.startsWith("ing",len-3) ) {
    ends = true;
  }
  return ends;
}
Why do we use "len-3" for the 2nd parameter?
Because the length of "ing" is 3.
This causes startsWith() to start checking beginning at the 3rd-to-last character.

If we wanted to check whether a String ends with "ment",
we would use len-4: str.startsWith("ment",len-4)
This causes startsWith() to start checking beginning at the 4th-to-last character.

If we wanted to check whether a String ends with "ed",
we would use len-2: str.startsWith("ed",len-2)
This causes startsWith() to start checking beginning at the 2nd-to-last character.

endsGerund("walking") → true
endsGerund("said") → false
endsGerund("ate") → false

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

public boolean endsGerund(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