Posts

Java snippet to count number of words in a sentence

Java code snippet to count number of words in a sentence

Java code snippet to count number of spaces in a string

Java program to count no of consonants in a string

Java Snippet to count number of vowels in a string

Java program to count no of letters in a string

import java.util.*;

class countLetters
{
    public static void main(String s[])
    {
        Scanner sc = new Scanner(System.in);
        int count = 0;
        char ch;

        System.out.println("Enter The String To Count No. Of Letters :  ");
        String str = sc.nextLine();

        for(int i=0;i<str.length();i++)
        {
                ch = str.charAt(i);
                if(ch!=' ')
                {
                    count++;
                }

        }
        System.out.println("The no of letters in given string is : " + count);
    }
}

Java program to check whether a year is leap or not

import java.util.*;

class leapYear
{
    public static void main(String s[])
    {
        Scanner sc = new Scanner(System.in);

        System.out.println("Enter a year to check whether it is leap year or not: ");
        int yr= sc.nextInt();

        if((yr % 4 == 0) && (yr % 100 == 0) && (yr % 400 == 0))
        {
            System.out.println("The year "+yr+" is a leap year.");
        }

        else
        {
            System.out.println("The year "+yr+" is not a leap year.");
        }

    }
}