forked from TestLeafPages/JavaPrograms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindingAll.java
More file actions
75 lines (54 loc) · 1.76 KB
/
FindingAll.java
File metadata and controls
75 lines (54 loc) · 1.76 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
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
package coding;
import org.testng.annotations.Test;
public class FindingAll extends BaseTestNg {
String test = "$$ Welcome to 1st Automation Interview $$ ";
char[] ch = test.toCharArray();
//public int letter = 0, space = 0, num = 0, specialChar = 0;
@Test(priority=1) // longest code
public void findingAll() {
for(int i = 0; i < test.length(); i++){
if(Character.isLetter(ch[i])){
letter ++ ;
}
else if(Character.isDigit(ch[i])){
num ++ ;
}
else if(Character.isSpaceChar(ch[i])){
space ++ ;
}
else{
specialChar ++;
}}
System.out.println("$$ Welcome to 1st Automation Interview $$ ");
System.out.println("letter: " + letter);
System.out.println("space: " + space);
System.out.println("number: " + num);
System.out.println("specialCharcter: " + specialChar);
}
@Test(priority=2) // using ASCII
public void findAllCharactesInString() {
for (int j = 0; j < ch.length; j++) {
if (((int)ch[j] >= 65 && (int)ch[j] <= 122)){
if (!((int)ch[j] >= 91 && (int)ch[j] <= 96)) {
letter = letter+1;
}
}else if (((int)ch[j] >= 48 && (int)ch[j] <= 57)) {
num = num+1;
}else {
specialChar ++;
}
}
System.out.println("letter count: " +letter);
System.out.println("number count:" + num );
System.out.println(" special character count:" +specialChar);
}
@Test(priority=3) // Simpler Coding
public void usingRegx() {
String alp = test.replaceAll("[^a-zA-Z]", "");
System.out.println(alp);
String onlyNumber = test.replaceAll("[^0-9]", ""); // other options is \\D
System.out.println(onlyNumber);
String spl = test.replaceAll("[0-9a-zA-Z ]", ""); //excluding space as well
System.out.println(spl);
}
}