In Java, Recursively Count Upper Case Characters

import java.util.Scanner;

public class Test2{

    private static int upCount(String line, int pos){
        char c = line.charAt(pos);
        int n = ('A' <= c && 'Z' >= c) ? 1 : 0;
        if(line.length()-1 > pos){
            n += upCount(line, pos+1);
        }
        return n;
    }

    public static void main(String[]args){
        Scanner in = new Scanner(System.in);
        System.out.print("Enter string: ");
        String line = in.nextLine();
        System.out.printf("No# of upper case: %d\n", upCount(line, 0));
    }
}
Download

Comments

Popular posts from this blog