Pages

Friday, 19 August 2016

Chapter 5 Exercise 50, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

*5.50 (Count uppercase letters) Write a program that prompts the user to enter a string and displays the number of the uppercase letters in the string.

Enter a string: Welcome to Java
The number of uppercase letters is 2

import java.util.Scanner;
 
public class ProgrammingEx5_50 {
 public static void main(String[] args) {
  Scanner input = new Scanner(System.in);
  System.out.print("Enter a string:");
  String s = input.nextLine();
  int numberOfUp = 0;
 
  for (int i = s.length() - 1; i >= 0; i--) {
   if ('A' <= s.charAt(i) && s.charAt(i) <= 'Z') {
    numberOfUp++;
   }
  }
 
  System.out.println("The number of uppercase letters is " + numberOfUp);
 }
 
}

No comments:

Post a Comment