Challenge: Basic String Manipulation

Develop an application for string manipulation, including counting characters, converting them to lowercase, and determining character occurrences using TDD.

We'll cover the following

Task and requirements

You’re given a string containing a sentence. Your task is to create a Java program that performs the following operations:

  1. Count the total number of characters in the sentence, excluding spaces.

  2. Convert the sentence to all lowercase characters.

  3. Determine and print the number of occurrences of a specific character in the sentence.

Here are the simplified requirements for this challenge:

  1. Create a Java class named StringManipulator with the following methods:

    1. countCharacters: Takes a string as input and returns the total number of characters in the sentence, excluding spaces

    2. convertToLowerCase: Takes a string as input and returns the sentence with all lowercase characters

    3. countOccurrences: Takes a string and a character as input and returns the number of times the character appears in the sentence

  2. Implement test cases for each of the above methods to verify their correctness.

  3. Ensure that the initial test cases fail before writing the implementation code in the StringManipulator class.

  4. Implement the StringManipulator class to make the test cases pass.

Note: Use assertions to compare the expected result with the actual result returned.

Try it yourself

Add or modify the code in the widget below to implement your solution.

Get hands-on with 1400+ tech skills courses.