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);
    }
}
E6

Write a program which takes a string from Scanner and counts how many ‘a’ alphabet is present in the string. For e.g., If the command line argument is “abundant” then the program should print 2. If the given string is “bold” then it is 0.

E7

Write a program called ArgumentSkipper that will print every other argument given on the command line. If the program was executed with the following in the command line:

java ArgumentSkipper one two three a b c d

the program would print

one
three
b
d

E8

Create a program which takes a string input and prints out the last three letters of the string.

E9

Write a program that aks for a String input and then it returns the reverse of the given string as the output. Do not use any builtin function or libraries for this task. You can use loops and conditionals.

results matching ""

    No results matching ""