Share on Google+Share on Google+

Count letters in a string.

Advertisement
This example counts letter present in string.

Description:

This tutorial demonstrate how to find number of letter exists in a string. The str.length() method find the length of string.

Code:

public class CheckLetterInString {
  public static void main(String[] args) {
    String str = "1a2b3c4d5e678f";
    int counter = 0;
    for (int k = 0; k < str.length(); k++) {
      if (Character.isLetter(str.charAt(k)))
        counter++;
    }
    System.out.println(counter + " letters.");
  }
}

Output:

Download this code

Advertisements

Advertisement

Posted on: June 28, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.