Home » Java programs » Java String programs

Java program to make first alphabet capital of each word in a string

Here, we are implementing java program that will read a string and makes first alphabet capital of each word in given string.
Submitted by IncludeHelp, on December 11, 2017

Given a string and we have to make first alphabet capital of each word in given string using java program.

Example:

    Input:
    Input string: we are looking for good writers.

    Output:
    Output string: We Are Looking For Good Writers.

Program to make first alphabet capital of each word in given string in java

import java.util.Scanner;

public class MakeCapitalFirstWordInLine 
{
	public static void main(String[] args)
	{
		// create object of scanner class.
		Scanner in = new Scanner(System.in);

		// enter sentence here
		System.out.print("Enter sentence here : ");
		String line = in.nextLine();
		String upper_case_line = ""; 

		// this is for the new line which is generated after conversion.
		Scanner lineScan = new Scanner(line); 
		while(lineScan.hasNext())
		{
			String word = lineScan.next(); 
			upper_case_line += Character.toUpperCase(word.charAt(0)) + word.substring(1) + " "; 
		}

		// print original line with output.
		System.out.println("Original sentence is : " +line); 
		System.out.println("Sentence after convert : " +upper_case_line.trim()); 
	}
}

Output

Enter sentence here : we are looking for good writers.
Original sentence is : we are looking for good writers.
Sentence after convert : We Are Looking For Good Writers.





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.