Count letters in a string.

This example counts letter present in string.

 
 

Share on Google+Share on Google+

Count letters in a string.

Ads

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

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