InitCap Program in java

Question:

Write a program to convert the first character of each word in a sentence to uppercase.

If the first character of each word in the given sentence is already in upper case, then print “First character of each word is already in uppercase”.

Sample Input 1: 
Enter the String:
Work hard to get what you like

Sample Output 1:

Work Hard To Get What You Like

Sample Input 2:
Enter the String:
Work Hard To Get What You Like

Sample Output 2:

First character of each word is already in uppercase

CODE:

import java.util.*;
public class InitCap
{
    public static void main (String[] args) {
        Scanner sc=new Scanner(System.in);
        
        System.out.println("Enter the String:");
        char[] str=sc.nextLine().toCharArray();
        int upps=0,word=0;
        int len=str.length;
        for(int i=0;i<len;i++)
        {
            if(i==0 || Character.isWhitespace(str[i-1]))
            {word++;
            
                if(Character.isLowerCase(str[i]))
                {
                    str[i]=Character.toUpperCase(str[i]);
                }
                else
                {
                    upps++;
                }
            }
        }
        
        if(word==upps)
        {
            System.out.println("First character of each word is already in uppercase");
        }
        
        else
        {
            for(char i : str)
            {
                System.out.print(i);
            }
        }
    }
}
public class Student 
{
    private int id;
    private String name;
    private int[] marks;
    private float average;
    private char grade;
    
    public void setId(int id)
    {
        this.id=id;
    }
    
    public int getId()
    {
        return this.id;
    }
    
    public void setName(String name)
    {
        this.name=name;
    }
    
    public String getName()
    {
        return this.name;
    }
    
    public void setMarks(int[] marks)
    {
        this.marks=marks;
    }
    
    public int[] getMarks()
    {
        return this.marks;
    }
    
    public void calculateAvg()
    {   float sum=0;
        
        for(int i=0;i<this.marks.length;i++)
        {
            sum+=this.marks[i];
        }
        
        average=(float)(sum/(this.marks.length));
        setAverage(average);
    }
    
    public void findGrade()
    {int flag=0;
        for(int i=0;i<this.marks.length;i++)
        {
            if(this.marks[i]<50)
            {
                flag++;
            }
        }
        if(flag>0)
        {
            grade='F';
        }
        else
        {
            if(this.average>=80 && this.average<=100)
            {
                grade='O';
            }
            else if(this.average>=50 && this.average<=79)
            {
                grade='A';
            }
            else
            {
                grade='F';
            }
        }
        setGrade(grade);
        
    }
    
    public void setAverage(float average)
    {
        this.average=average;
    }
    
    public void setGrade(char grade)
    {
        this.grade=grade;
    }
    
    public float getAverage()
    {
        return this.average;
    }
    
    public char getGrade()
    {
        return this.grade;
    }
}
Next:
  1. Array Compatiblilty
  2. Sum of the maximum and the minimum element
  3. String Concatenation

Leave a Comment

Scroll to Top