Write a program with a for loop that prompts the user to ent
Write a program with a for loop that prompts the user to enter a string and displays the number of uppercase letters in the string in Java
Solution
StringUpper.java
import java.util.Scanner;
public class StringUpper {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String s = \"\";
for(;;){
System.out.print(\"Enter the string(stop to Quit): \");
s = scan.nextLine();
if(s.equalsIgnoreCase(\"stop\")){
break;
}
else{
int count = 0;
for(int i=0; i<s.length(); i++){
char ch = s.charAt(i);
if(ch >= \'A\' && ch <= \'Z\'){
count++;
}
}
System.out.println(\"The number of uppercase letters in the string is \"+count);
}
}
}
}
Output:
Enter the string(stop to Quit): Suresh Murapaka
The number of uppercase letters in the string is 2
Enter the string(stop to Quit): Sekhar
The number of uppercase letters in the string is 1
Enter the string(stop to Quit): DuMMy
The number of uppercase letters in the string is 3
Enter the string(stop to Quit): anshu
The number of uppercase letters in the string is 0
Enter the string(stop to Quit): stop
