forked from bipeensinha/firstJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringoperations29sep2023.java
More file actions
89 lines (62 loc) · 2.54 KB
/
stringoperations29sep2023.java
File metadata and controls
89 lines (62 loc) · 2.54 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//1) Salatations removal from the name
//2) Replacement of some special characters
//3) Removal of some characters
//4) Swapping certain names
//Total 7 steps mentioned in the below comments.
public class Main {
public static void main(String[] args) {
String string1 = " Thambi ";
String string2 = " Thambi Subbaiah ";
//String string1="Thambi";
//String string2="Thambi Subbaiah";
// String string1=" Mr. Vinit Kumar ";
// String string2=" Vinitkumar ";
//1. trim the begin and end - spaces
string1 = string1.trim();
string2 = string2.trim();
//2. replace any number of spaces with 1 space
string1 = string1.replaceAll(" +", " ");
string2 = string2.replaceAll(" +", " ");
System.out.println(string1);
System.out.println(string2);
//3. replace single quote with null
string1 = string1.replaceAll("\'","");
string2 = string2.replaceAll("\'","");
//4. replace hyphen with 1 space
string1 = string1.replaceAll("-"," ");
string2 = string2.replaceAll("-"," ");
//5. replace . with 1 space
string1 = string1.replace("."," ");
string2 = string2.replace("."," ");
String[] salutations = {"Miss", "Miss.", "Mr", "Mr.", "Mrs", "Mrs.", "Ms", "Ms."};
string1 = string1.trim();
//6. Salutation removal
for(String name : salutations)
{
if (string1.startsWith(name)){
System.out.println(name);
string1=string1.substring(name.length()).trim();
break;
}
}
for(String name : salutations)
{
if (string2.startsWith(name)){
System.out.println(name);
string2=string2.substring(name.length()).trim();
break;
}
}
//find word length
int string1wordlength = string1.split(" ").length;
int string2wordlength = string2.split(" ").length;
//7. logic for swapping
if ((string1.replaceAll(" ", "").length() == string2.replaceAll(" ", "").length() && string1wordlength > string2wordlength) || (string1.replaceAll(" ", "").length() < string2.replaceAll(" ", "").length() && string1wordlength < string2wordlength)){
string1 = string1 + string2;
string2 = string1.substring(0, string1.length() - string2.length());
string1 = string1.substring(string2.length());
}
System.out.println(string1);
System.out.println(string2);
}
}