Lesson 5.1 · Write it
Last character
A functionPython or Java7 tests, 4 of them hidden
On a phone? Put the lines in order instead
Write a function that returns the last character of a text value.
texthas at least one character. It contains only English letters, digits, spaces and punctuation.- Return the last character as a text value of length 1. In Java that means a
String, not achar.
Only the value your function returns is checked.
def last_char(text: str) -> str
Examples
Your function is tested on 7 cases: the 3 examples below, and 4 hidden ones that check the rules above.
- last_char("hello") should return "o": the last of five letters (rule 2)
- last_char("A") should return "A": a single character (rule 1)
- last_char("go ") should return " ": a space is a character too, and here it is the last one
public static String lastChar(String text)
Running Java here comes later. You can put this answer's lines in order instead. You can still write your Java answer (it stays while this page is open) and compare it with our worked solution. Or try this exercise in Python.Checking whether Java answers can run here.
Examples
Your function is tested on 7 cases: the 3 examples below, and 4 hidden ones that check the rules above.
- lastChar("hello") should return "o": the last of five letters (rule 2)
- lastChar("A") should return "A": a single character (rule 1)
- lastChar("go ") should return " ": a space is a character too, and here it is the last one