Computer Applications

Write a program in Java to accept a string in lowercase and change the first letter of every word to uppercase. Display the new string.

Sample input: we are in cyber world
Sample output: We Are In Cyber World

Java

Java String Handling

ICSE 2018

163 Likes

Answer

import java.util.Scanner;

public class KboatString
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter a sentence:");
        String str = in.nextLine();
        String word = "";

        for (int i = 0; i < str.length(); i++) {
            if (i == 0 || str.charAt(i - 1) == ' ') {
                word += Character.toUpperCase(str.charAt(i));
            }
            else {
                word += str.charAt(i);
            }
        }

        System.out.println(word);
    }
}

Output

BlueJ output of Write a program in Java to accept a string in lowercase and change the first letter of every word to uppercase. Display the new string. Sample input: we are in cyber world Sample output: We Are In Cyber WorldBlueJ output of Write a program in Java to accept a string in lowercase and change the first letter of every word to uppercase. Display the new string. Sample input: we are in cyber world Sample output: We Are In Cyber World

Answered By

56 Likes


Related Questions