Write a program in Java to input a string and count the number of words starting with an uppercase alphabet. Also, display the count.
Answers
Answered by
6
import java.util.Scanner;
class Idkanything
{
public static void main (String args [])
{
Scanner sc = new Scanner (System.in);
System.out.println("Enter a String.");
String s = sc.nextLine();
String str = s.trim();
str = " " + str;
int len = str.length();
int count = 0;
for(int i=0; i<=len-2; i++)
{
char ch1 = str.charAt(i);
char ch2 = str.charAt(i+1);
if(ch1==' ' && ch2>='A' && ch2<='Z')
count++;
}
System.out.println("Count:"+count);
}
}
Attachments:
Similar questions
Physics,
14 days ago
English,
14 days ago
Chemistry,
29 days ago
Science,
29 days ago
Social Sciences,
8 months ago
Math,
8 months ago
India Languages,
8 months ago