Java Program to Capitalize First Alphabet of Every Word in a String

(234 Views)


In this problem statement you have to write a java program to Capitalize First alphabet of every word in the String.
For eg.
Input: hello World this Is My world
Output: Hello World This Is My World

Capitalize First Alphabet in a String
public class CapitalizeFirst { public void LetterCapitalize(String sentence) { char sen[]=sentence.toCharArray(); if(sen[0]>96) // checking if First alphabet of First word is a small alphabet sen[0]=(char)(sen[0]-32); // subtract ascii value of First alphabet with 32 to convert it into Capital Alphabet for(int i=0;i<sen.length;i++) { if(sen[i]==' ') //checking for space occurrence if((int)sen[i+1]>=97) //checking if first alphabet is not Already capital sen[i+1]=(char)(sen[i+1]-32); } for(int i=0;i<sen.length;i++) // Output Printing System.out.print(sen[i]); } public static void main(String[] args) { CapitalizeFirst c=new CapitalizeFirst(); c.LetterCapitalize("hello World this Is My world"); // Input String } }

Solution Worked 4 UpvotesUpvote

        

Solution Didn't Worked 1 DownvotesDownvote



Comments



Search