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

 

orion.a.smith@gmail.com apcsa-loops > apcsaChatbotsLastWordHard
prev  |  next  |  chance

HARDER PROBLEM: skip if you have trouble
Given an input String, find and return the last word in the input. A word always ends in a letter, and after the letter is zero or more periods, followed by one or more spaces unless the word is the end of the String. So "don't" is actually a word for this purpose, but not "teachers'" (apostrophe at the end means it isn't a letter). "blah." is a word ending in the period, for which this method should just return "blah". Precondition: the input will have at least one word.

HINT: use Character.isLetter(str.charAt(index)) to find out the location of the last letter.  You probably need to use a loop from the back, unless you are being all fancy.

Super-advanced challenge: what is the minimum number of statements you can use to solve this?

apcsaChatbotsLastWordHard("We are the world") → "world"
apcsaChatbotsLastWordHard("We are the world.") → "world"
apcsaChatbotsLastWordHard("We are. the world") → "world"

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

public String apcsaChatbotsLastWordHard(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: 360

Copyright Nick Parlante 2017 - privacy