-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLastChar.java
More file actions
28 lines (17 loc) · 922 Bytes
/
LastChar.java
File metadata and controls
28 lines (17 loc) · 922 Bytes
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
// Description: Write a program that reads a string from the user and prints the last character of each word in the string.
// If the last character is a letter, print it. If the last character is not a letter, do not print it. Assume that the string contains only letters and spaces. For example,
//if the user enters "Hello, world!", the program should print "o".
import java.util.*;
public class LastChar {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String x = sc.nextLine();
String[] words = x.split(" "); // Split the string into words using the space character as a delimiter
for (int i = 0; i < words.length; i++) {
char ch = words[i].charAt(words[i].length() - 1); // Get the last character of the word
if (Character.isAlphabetic(ch)) {
System.out.print(ch);
}
}
}
}