forked from SLIIT-FacultyOfComputing/sliit-faculty-of-computing-se2012-ooad-practical-02-SE2012-OOAD-Practical_002
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StringManipulation.java
55 lines (41 loc) · 1.92 KB
/
StringManipulation.java
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
import java.util.Scanner;
public class StringManipulation {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first name: ");
String firstName = sc.nextLine();
System.out.print("Enter middle name: ");
String middleName = sc.nextLine();
System.out.print("Enter last name: ");
String lastName = sc.nextLine();
StringBuilder sb = new StringBuilder();
sb.append(lastName).append(" ").append(firstName).append(" ").append(middleName);
String fullName = sb.toString();
System.out.println("Your full name is: " + fullName);
System.out.print("\nEnter another full name: ");
String secondFullName = sc.nextLine();
if (secondFullName.equalsIgnoreCase(fullName)) {
System.out.println("The two names are the same.");
} else {
System.out.println("The two names are different.");
}
// Part 03
System.out.println("\n** Replacing 'a' with '@' and 'e' with '3' **\n");
String replacedName = fullName.replace('a', '@').replace('e', '3');
System.out.printf("Name after replacement: %s\n", replacedName);
System.out.println("\n** Converting to upper case **\n");
String upperCaseName = fullName.toUpperCase();
System.out.println("Your full name in upper case is: " + upperCaseName);
// Part 04
System.out.println("\n** Splitting the name **");
String[] parts = fullName.split(" ");
System.out.printf("\n\t%s\n\t%s\n\t%s", parts[0], parts[1], parts[2]);
// Part 05
System.out.println("\n\n** Trimming spaces **");
System.out.print("Enter a string with leading and trailing spaces: ");
String str2 = sc.nextLine();
String trimmedStr2 = str2.trim();
System.out.println("Trimmed string is: " + trimmedStr2);
sc.close();
}
}