Computer Applications

Write a program in Java to accept a String from the user. Pass the String to a method Change(String str) which displays the first character of each word after changing the case (lower to upper and vice versa).
Sample Input: Delhi public school
Sample Output:
d
P
S

Java

User Defined Methods

34 Likes

Answer

import java.util.Scanner;

public class KboatStringChange
{
    public void change(String str) {

        String t = " " + str;
        int len = t.length();

        for (int i = 0; i < len - 1; i++) {
            if (t.charAt(i) == ' ') {
                char ch = t.charAt(i+1);
                if (Character.isUpperCase(ch))
                    ch = Character.toLowerCase(ch);
                else if (Character.isLowerCase(ch))
                    ch = Character.toUpperCase(ch);
                System.out.println(ch);
            }
        }
    }

    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter a string: ");
        String s = in.nextLine();

        KboatStringChange obj = new KboatStringChange();
        obj.change(s);
    }
}

Variable Description Table

Program Explanation

Output

Answered By

12 Likes


Related Questions