Table of Contents
Project 10: Answer
import java.util.Scanner;
public class SnowballStringChecker {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a sentence:");
String sentence = sc.nextLine().trim();
// Check if the sentence ends with '.' or '?'
if (!(sentence.endsWith(".") || sentence.endsWith("?"))) {
System.out.println("INCORRECT TERMINATING CHARACTER. INVALID INPUT");
return;
}
// Remove the terminating character for processing
sentence = sentence.substring(0, sentence.length() - 1).trim();
// Split words by one or more spaces
String[] words = sentence.split("\\s+");
boolean isSnowball = true;
for (int i = 1; i < words.length; i++) {
// Check if current word length is exactly 1 more than previous word length
if (words[i].length() != words[i - 1].length() + 1) {
isSnowball = false;
break;
}
}
if (isSnowball) {
System.out.println("IT IS A SNOWBALL STRING");
} else {
System.out.println("IT IS NOT A SNOWBALL STRING");
}
}
}
| Input | Output |
|---|---|
| He may give bonus. | IT IS A SNOWBALL STRING |
| Is the cold water frozen? | IT IS A SNOWBALL STRING |
| Look before you leap. | IT IS NOT A SNOWBALL STRING |
| The child is father of the man! | INCORRECT TERMINATING CHARACTER. INVALID INPUT |