Kindergarten Counting Game
Everybody sit down in a circle. Ok. Listen to me carefully.| Kindergarten Counting Game |
``Woooooo, you scwewy wabbit!''
Now, could someone tell me how many words I just said?
Input and Output
Input to your program will consist of a series of lines, each line containing multiple words (at least one). A ``word'' is defined as a consecutive sequence of letters (upper and/or lower case).Your program should output a word count for each line of input. Each word count should be printed on a separate line.
Sample Input
Meep Meep! I tot I taw a putty tat. I did! I did! I did taw a putty tat. Shsssssssssh ... I am hunting wabbits. Heh Heh Heh Heh ...
Sample Output
2 7 10 9
Ex: hi!hello@world~
Answer:3
import java.util.Scanner;
public class UVA494 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int count = 0;
while (sc.hasNext()) {
count = 0;
int record = 0;
String string = sc.nextLine();
for (int i = 0; i < string.length(); i++) {
if ((string.charAt(i) >= 65 && string.charAt(i) <= 90)
|| (string.charAt(i) >= 97 && string.charAt(i) <= 122)) {
record = 1;
} else {
count += record;
record = 0;
}
}
System.out.println(count);
}
sc.close();
}
}
留言
張貼留言