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:

E1

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

E2

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'.

E3

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.

E4

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

E5

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;

        System.out.println(printedMessage);
    }
}

results matching ""

    No results matching ""