Tuesday, 18 February 2014

String character counting using for loop

public class CountCharacters {

    public static void main(String args[])
    {
        
        String input = "Today is tuesday"; //count number of "a" on this String.
        int charCount = 0;
        for(int i =0 ; i<input.length(); i++){
            if(input.charAt(i) == 'd'){
                charCount++;
            }
        }
        System.out.println("count of character 'd' on String: 'Today is Monday' using for loop  " + charCount);
}
}

No comments:

Post a Comment