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

 

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

Given an input String str, determine if searchWord exists as its own word in the input. For this problem, a word means any characters that are surrounded by spaces, or are at the beginning of the input (space after, starts at 0), or the end of the input (space before, ends at str.length() - 1). Precondition: str will contain only capital letters and spaces (no punctuation or other characters), and searchWord will contain only capital letters.


HINT: You will need to check for searchWord at the beginning and end of str, as well as in the middle.  Most students will probably use three if statements to address these three possibilities.

CHALLENGE: for advanced students, you can also do this by using split() and a for loop.  (requires arrays)  Or you can use a while loop that looks over and over again for the next occurrence of searchWord within str.

CHALLENGE: for really advanced students, solve this problem using a Scanner, regular expressions or stream processing.

CHALLENGE (actually not that hard): solve this in one statement.

apcsaChatbotsWordExists("SAM I AM", "I") → true
apcsaChatbotsWordExists("SAM I AM", "SA") → false
apcsaChatbotsWordExists("SAM I AM", "SAM") → true

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

public boolean apcsaChatbotsWordExists(String str, String searchWord) { }

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: 210

Copyright Nick Parlante 2017 - privacy