Pages

Tuesday, 23 April 2013

Java code to check number of uppercase letters, lowercase letters, numerals, vowels, spaces & special characters in a String

Java program to display number of Uppercase letters, Lowercase letters, Numerals, Vowels, Spaces and Special characters contained in a string entered by the user.

import java.io.*;
class StringInfo
{
 static String n;
 static int l;
 public static void main(String args[]) throws IOException
 {
 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 // Read the String
 System.out.print("Enter a String : ");
 n = br.readLine();
 l = n.length();
 find();
 }
 public static void find()
 {
 int a=0,b=0,c=0,d=0,e=0;
 char ch;
 for(int i=0;i<l;i++)
 {
  ch = n.charAt(i);
  if(ch>=65 && ch<=90) // Condition for Uppercase letters
  a++;
  if(ch>='a' && ch <='z')
  b++;
  if(ch>='0' && ch<='9')
  c++;
  if(ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U' ||
  ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u')
  d++;
  if(ch==' ') // Condition for spaces
  e++;
        }
 System.out.println("\nNo. of Uppercase letters = " +a);
 System.out.println("\nNo.of Lowercase letters = " +b);
 System.out.println("\nNo. of Numerals = " +c);
 System.out.println("\nNo. of Vowels = " +d);
 System.out.println("\nNo. of Spaces = " +e);
 System.out.println("\nNo. of Special Characters = "+(l-(a+b+c+e)));
 }
}

ALGORITHM:-

1. Start

2. Accept a sentence from the user.

3. Extract each character from the sentence.

4. Check whether the character is in uppercase or lowercase

5. Count the number of digits, special characters and the number of words in the sentence.

6. Print the information.

7. End

OUTPUT:-

String Info in Java


Enter a String : The nvidia 3D Vision costs about $200 !

No. of Uppercase letters = 3

No. of Lowercase letters = 23

No. of Numerals = 4

No. of Vowels = 11

No. of Spaces = 7

No. of Special Characters = 2

Related Programs:-

Print the Initials of a name

Find the frequency of each character in a string

Arrange the letters of a word in alphabetical order

Find the pig Latin equivalent of a word

Change Lowercase letters to Uppercase and vice-versa in a string

1 comment:

  1. This comment has been removed by a blog administrator.

    ReplyDelete

Back to Top