Count alphabets in a String in Java

Counting Alphabets not all characters!

An example on counting only alphabets in a string in Java.

Let's count!

* Taken from gowtham.gutha.util.StringOps - Useful for performing String operations.
* Copyright (C) 2012  Gowtham Gutha. All Rights Reserved.
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

// Import for Scanner class
import java.util.*;
class CountAlphabets

public static void main(String args[])

// Create Scanner object
Scanner s=new Scanner(;

// Read a string from user and store it in st
String st=s.nextLine();

// Intialize the variable 'count' to '0'
int count=0;

// Convert the given string into char array
char[] c=st.toCharArray();

// Loop till end of string
for(int i=0;i<st.length();i++)

// Get the ascii value of each character
int k=(int)c[i];

// Check if char lies in A-Z, a-z, go inside only if so..

// Increment the variable count

return count;


private static boolean liesIn(int k)

// If char ascii value lies between 90-96 (including 90,96), then return true
return true;

// Return false, if it does not contain chars with ascii values 90-96
else return false;



Feel free to drop a comment for further help. 

Posted by at on

Tags: String Handling,