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

 

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

Given an input String (made up of characters), find and return the single-character String representing the "highest" character in the input (i.e., the character which comes later in an ordering of characters than all others). Precondition: str will have at least one character.


There are several ways to extract a single character from a String.  You could definitely use str.charAt(i), which returns the char primitive at index i.  char primitives can be directly compared like numbers using relational operators > and <, which makes the "find the max" part easier. Also be careful how you return a char, as you may accidentally return a number.  Or, you could get a single-character substring by taking advantage of a for loop.  str.substring(i,i+1) will accomplish this feat.  If you work with substring it will return a String, and you need to compare one String with another using compareTo.

Never done a "find the max"/"find the min" problem before?  The typical solution is to assume that one item in a set (in this case, e.g. the first character) is the largest.  Keep a variable to store the largest item found so far, compare that to each successive item in the collection (each character in the String here) and update your assumption as you find a new candidate.

apcsaLoopsMaxCharacter("ab") → "b"
apcsaLoopsMaxCharacter("Xena") → "n"
apcsaLoopsMaxCharacter("1234567890") → "9"

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

public String apcsaLoopsMaxCharacter(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: 210

Copyright Nick Parlante 2017 - privacy