Exercise: String operations

Write a program that implements a couple of string operations using methods: Use the following code in your main method to test your methods:
System.out.println("'World' starts with 'W': " + startsWith("World", 'W'));
System.out.println("'World' starts with 'o': " + startsWith("World", 'o'));

char[] haystack = { 'H', 'e', 'l', 'l', 'o' };
char needle = 'l';
System.out.println(toString(haystack) + " contains '" + needle + "' : " +
    contains(haystack, needle));

char[] forbiddenChars = { 'a', 'e', 'i', 'o', 'u' };
String stringToCensor = "This is a test string with vowels";
String censoredString = censor(stringToCensor, forbiddenChars);
System.out.println("Censored string: " + censoredString);

String sub = subString("Hello World", 0, 5);
System.out.println("Substring: '" + sub + "'");

String needsTrimming = "   Hello World   ";
System.out.println("Trimmed string: '" + trim(needsTrimming) + "'");

This should produce the following output:

'World' starts with 'W': true
'World' starts with 'o': false
H, e, l, l, o contains 'l' : true
Censored string: Th*s *s * t*st str*ng w*th v*w*ls
Substring: 'Hello'
Trimmed string: 'Hello World'

Implementation

You are not allowed to use any methods of String from the Java standard library, except these:

Your methods are allowed to call each other.

Hints 💡

Hand in instructions

  1. Make sure your program runs correctly.
  2. Follow Java naming conventions of variables.
  3. Make sure your program is well-formatted.
  4. Hand in your program by uploading Main.java to Moodle.