-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAntakshari.java
35 lines (30 loc) · 1.14 KB
/
Antakshari.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/*next word should start at the same letter previous word ended*/
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Antakshari {
public static void main(String[] args) {
List<String> words = new ArrayList<>();
try (Scanner obj = new Scanner(System.in)) {
System.out.println("Enter the starting word:");
String startingWord = obj.nextLine();
words.add(startingWord);
while (true) {
System.out.println("Enter a word:");
String word = obj.nextLine();
if (word.isEmpty()) {
break;
}
if (word.charAt(0) != words.get(words.size() - 1).charAt(words.get(words.size() - 1).length() - 1)) {
System.out.println("Invalid word! The word should start with the last letter of the previous word.");
continue;
}
words.add(word);
}
}
System.out.println("Actual chain of words:");
for (String word : words) {
System.out.println(word);
}
}
}