Count the freequency of Words starting with Capital Letter in a sentence.
import java.util.*;
public class Capital {
String sent;
int freq;
void input() {
Scanner sc = new Scanner(System.in);
System.out.print("Enter Sentence: ");
sent = sc.nextLine();
sc.close();
}
void display() {
StringTokenizer st = new StringTokenizer(sent, " .");
while (st.hasMoreTokens())
if (Character.isUpperCase(st.nextToken().charAt(0)))
freq++;
System.out.println("Sentence: " + sent);
System.out.println("Freequency: " + freq);
}
public static void main(String[] args) {
Capital a = new Capital();
a.input();
a.display();
}
}
| Name | Type | Uses |
|---|---|---|
| global | ||
| sent | String | to store entered sentence |
| freq | int | to store freequeny of words starting with capital letters |
| void input() | ||
| sc | Scanner | object to take user input |
| void display() | ||
| st | StringTokenizer | object used to extract words from the senetence |
| void main() | ||
| a | Capital | Object to call its classes |