LearnFrenzy
Sign in Take a free test

Strings and StringBuilder · Exercise 4 of 4

Strings and StringBuilder Java String Programs

  • 10 questions
  • Page 1 of 1
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Classic Java string programs asked in interviews and placement tests. Write each one yourself first, then open the solution – every solution was compiled and run on Java 21, and the output shown is the real output.

Question 1 Easy

Print how many times each character (ignoring spaces) appears in a string, in the order the characters first appear.

Question page & discussion
Question 2 Medium

Find the first character that appears exactly once in each input string, or report that there is none, using a 26-letter count array and two passes.

Question page & discussion
Question 3 Medium

Compress strings like "aaabccdddd" to "a3b1c2d4"; return the original string if the compressed version is not shorter.

Question page & discussion
Question 4 Easy

Convert sentences to title case – first letter of every word uppercase, the rest lowercase – collapsing extra spaces.

Question page & discussion
Question 5 Easy

Toggle the case of every letter in a string and count uppercase letters, lowercase letters, digits, spaces and other characters.

Question page & discussion
Question 6 Easy

Reverse the letters of every word in a sentence while keeping the words in their original order and single spaces between them.

Question page & discussion
Question 7 Medium

Find the longest common prefix of an array of strings by shortening a candidate prefix until every string starts with it.

Question page & discussion
Question 8 Medium

Read a paragraph from input and print the number of words, sentences (ending in . ! or ?), letters and the average word length.

Question page & discussion
Question 9 Easy

Remove repeated characters from strings, keeping only the first occurrence of each, using a boolean array for seen characters.

Question page & discussion
Question 10 Medium

Encrypt text with a Caesar cipher (shift letters by k, wrapping around, keeping case and leaving other characters unchanged), then decrypt it back.

Question page & discussion
All exercises doneBack to Strings and StringBuilderRevise the concepts or pick another topic

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links