Exercise (Solutions)

You always refer to Javadoc to learn more about any class. Here is the link to the javadoc for Java 9, String class: https://docs.oracle.com/javase/9/docs/api/java/lang/String.html

Refer to the javadoc and complete the following:


Write a program to convert any alpha string given as a program argument to lowercase.


Write a program to find if a given program argument has a sequence of letters 'cat' anywhere in the string. If you find 'cat' in the string, then print out the index position of the last occurrence of 'cat'.


Write a program that computes your initials from your full name and displays them. The first name, middle name and last name strings are separated by a space and given as input from the command line (program arguments). If you do not have a middle name, assume your middle name is 'Java' temporarily! Hint: explore charAt method on String.


Write a program that reverses the sequence of a String given as a program argument. For e.g, input to the program would be “fantastic” and the output from the program should be “citsatnaf”. You could also use a Scanner instead of a program argument. Hint: Look into the documentation of StringBuilder class: https://docs.oracle.com/javase/9/docs/api/java/lang/StringBuilder.html


Modify the below program so that empty spaces which are present at the end of message1 are not printed out. Study the documentation and use one of the string methods to remove the white spaces without changing the message string itself.

public class MyClass{

    public static void main(String[] args){

        String message1 = " This is an easy lesson     ";
        String message2 = "!!!";
        String printedMessage = message1 + message2;


results matching ""

    No results matching ""