-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextra_examples
129 lines (123 loc) · 4.34 KB
/
extra_examples
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import java.util.Scanner;
public class String_Practice_Examples {
// extra examples on Java string and array 2D
public static void main(String[] args) {
// TODO: check if an email is valid or not
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("Enter an email (or type 'exit' to quit): ");
String email = scanner.nextLine();
if (email.equalsIgnoreCase("exit")) {
System.out.println("Goodbye!");
break;
}
try {
checkEmail(email); // Validate
String masked = maskEmail(email);
System.out.println("Original: " + email);
System.out.println("Masked: " + masked);
} catch (IllegalArgumentException e) {
System.out.println("Error: " + e.getMessage());
}
}
scanner.close();
// TODO: word and letter counter
String text = "Java is powerful! 123";
int wordCount = countWords(text);
int letterCount = countLetters(text);
System.out.println("Total words: " + wordCount);
System.out.println("Total letters: " + letterCount);
// TODO: search word in 2d array rows or columns
char[][] grid = {
{'J', 'A', 'V', 'A'},
{'P', 'Y', 'T', 'H'},
{'C', 'P', 'H', 'P'},
{'R', 'U', 'B', 'Y'}
};
String word = "JAVA";
boolean found = findWordInGrid(grid, word);
if (!found) {
System.out.println("Word \"" + word + "\" not found in grid.");
}
}// end of main method
// TODO: check email validity mask the email method
public static void checkEmail(String email) {
if (email == null || email.trim().isEmpty()) {
throw new IllegalArgumentException("Email cannot be empty.");
}
email = email.trim();
if (!email.contains("@") || !email.contains(".") ||
email.indexOf("@") > email.lastIndexOf(".") ||
email.startsWith("@") || email.endsWith(".") ||
email.length() < 5) {
throw new IllegalArgumentException("Invalid email format.");
}
}
public static String maskEmail(String email) {
email = email.trim();
int atIndex = email.indexOf('@');
if (atIndex <= 1) {
return "***" + email.substring(atIndex);
}
char first = email.charAt(0);
char last = email.charAt(atIndex - 1);
String domain = email.substring(atIndex);
return first + "***" + last + domain;
}
// TODO: word and letter counter methods
public static int countWords(String text) {
int count = 0;
boolean lastWasSpace = true;
for (int i = 0; i < text.length(); i++) {
char ch = text.charAt(i);
if (Character.isLetterOrDigit(ch)) {
if (lastWasSpace) {
count++;
lastWasSpace = false;
}
}
else {
lastWasSpace = true;
}
}
return count;
}
public static int countLetters(String text) {
int count = 0;
for (int i = 0; i < text.length(); i++) {
char ch = text.charAt(i);
if (Character.isLetter(ch)) {
count++;
}
}
return count;
}
// TODO: search 2d array for a word method
public static boolean findWordInGrid(char[][] grid, String word) {
int rows = grid.length;
int cols = grid[0].length;
// Check each row
for (int i = 0; i < rows; i++) {
String row = "";
for (int j = 0; j < cols; j++) {
row += grid[i][j];
}
if (row.contains(word)) {
System.out.println("Word \"" + word + "\" found in row " + i);
return true;
}
}
// Check each column
for (int j = 0; j < cols; j++) {
String col = "";
for (int i = 0; i < rows; i++) {
col += grid[i][j];
}
if (col.contains(word)) {
System.out.println("Word \"" + word + "\" found in column " + j);
return true;
}
}
return false;
}
}