-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathProblem_20.java
More file actions
33 lines (27 loc) · 1.12 KB
/
Problem_20.java
File metadata and controls
33 lines (27 loc) · 1.12 KB
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
package strings;
//* Convert a Sentence into its equivalent mobile numeric keypad sequence.
public class Problem_20 {
public static String convert(String sentence) {
sentence = sentence.toUpperCase(); // Convert to uppercase for consistent mapping
StringBuilder numberSequence = new StringBuilder();
for (char c : sentence.toCharArray()) {
if (Character.isLetter(c)) {
int digit = (c - 'A') / 3 + 2; // Map letter to corresponding number on keypad
// Add extra press for letters on 7, 8, and 9
if (digit == 8 || digit == 9) {
digit++;
}
numberSequence.append(digit);
} else if (c == ' ') {
numberSequence.append(0); // Add 0 for space
}
// Ignore other characters
}
return numberSequence.toString();
}
public static void main(String[] args) {
String sentence = "Welcome to Coursewave!";
String numberSequence = convert(sentence);
System.out.println(sentence + " converted to: " + numberSequence);
}
}