Author Topic: Java program to count number of alphabets and non-alphabets in a string  (Read 966 times)

kathir

  • Administrator
  • Sr. Member
  • *****
  • Posts: 283
Here is the C# program to count number of alphabets and non-alphabets in a string.

Code: (java)
//Source Code Listing

import java.io.*;
import java.lang.*;
import java.util.*;
 
class Count_Alphabet
{
      static public String ReadString()
      {
            try
            {
                  String inpString = "";
                  InputStreamReader input = new InputStreamReader(System.in);
                  BufferedReader reader = new BufferedReader(input);
                  return reader.readLine();
            }
            catch (Exception e)
            {
                  e.printStackTrace();
            }
            return "";
      }
 
      public static void main(String[] args)
      {
            System.out.println("Program to count the number of alphabets and non-alphabets");
            System.out.print("Enter a string:");
            String input = ReadString();

            int alphabet_count = 0;
            int non_alphabet_count = 0;

            for(int i = 0; i < input.length(); i++)
            {
               if(input.charAt(i) >= 'a' && input.charAt(i) <= 'z')
                  alphabet_count++;
               else if(input.charAt(i) >= 'A' && input.charAt(i) <= 'Z')
                  alphabet_count++;
               else 
                  non_alphabet_count++;
            }            
            
            
            System.out.format("Number of alphabets: %d\n", alphabet_count);
            System.out.format("Number of Non-alphabets: %d\n", non_alphabet_count);
      }
}

output:

Program to count the number of alphabets and non-alphabets
Enter a string:welcome to software & finance
Number of alphabets: 24
Number of Non-alphabets: 5


 


Disclaimer: This web site is for educational and informational purposes only. Click here to read the Disclaimer.
Content copyright 2010-2014. Kathiresan. All rights reserved.
This page is using SMF version 2.0.1