LearnFrenzy
Sign in Take a free test

Java Programming · Topic 5 of 28

Strings and StringBuilder

  • 4 exercises
  • 60 questions
  • ~60 min
  • Answers explained

Programs & Examples

Strings and StringBuilder · Study material

Word and vowel statistics of a sentence

toLowerCase, replaceAll with a regex, split on whitespace and indexOf combine into a small text-analysis program.

public class Main {
    public static void main(String[] args) {
        String text = "Java is easy to learn. Java is powerful, and Java is everywhere!";
        String[] words = text.toLowerCase().replaceAll("[^a-z ]", "").split("\\s+");

        int vowels = 0;
        for (char ch : text.toLowerCase().toCharArray()) if ("aeiou".indexOf(ch) >= 0) vowels++;

        String longest = "";
        int javaCount = 0;
        for (String w : words) {
            if (w.length() > longest.length()) longest = w;
            if (w.equals("java")) javaCount++;
        }
        String capitalised = Character.toUpperCase(longest.charAt(0)) + longest.substring(1);
        System.out.println("words: " + words.length + ", vowels: " + vowels + ", 'java' x" + javaCount + ", longest: " + capitalised);
    }
}

Output

words: 12, vowels: 22, 'java' x3, longest: Everywhere

StringBuilder editing operations

The StringBuilder example chains append, insert, replace, delete, reverse and setCharAt on one mutable object.

JSON with a text block

The text-block example builds a JSON document with formatted() – no escaped quotes or newline characters needed.

Ready to test your Programming?

Timed online tests with instant scores and explanations.

About this section

About Java Programming

Java is one of the most used languages in the world – for enterprise software, Android apps, banking systems and backend services. This section covers core Java from syntax and data types to OOP, strings, arrays, exceptions, collections and multithreading, with clear explanations and practice questions for every topic.

Why it matters

  • Java is the main language in service-company placement tests and many product companies
  • OOP, String, collections and exception questions appear in nearly every Java interview
  • Strong core Java is the base for Spring Boot, Android and backend development
  • Java questions are common in university exams and certification tests

Who it is for

  • School students (Class 11–12 / ICSE computer applications) learning Java
  • College students (B.Tech, BCA, MCA) preparing for exams, labs and projects
  • Freshers preparing for placements, Java developer roles and technical interviews
  • Developers revising core Java before an interview

What every topic gives you

  • Study material in five tabs: concepts with runnable examples, coding techniques, programs, interview questions and a cheat sheet
  • MCQs that check your understanding of every concept
  • "Predict the output" puzzles – the trickiest part of written tests
  • "Find the error" programs that train you to spot bugs quickly
  • Complete programs with solutions and real output, verified on a compiler

Asked in

  • Campus placements
  • Java developer interviews
  • Service company tests
  • University exams
  • Certification exams
  • Government IT exams

How to use this section

  1. Read the study material of a topic and run the examples yourself.
  2. Practise the four exercises – MCQs, output, errors, programs – and read every explanation.
  3. Test yourself with the timed online test, then revise the topics you got wrong.

Search LearnFrenzy

Esc

Quick links