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
/
StringBasic.java
92 lines (76 loc) · 2.67 KB
/
StringBasic.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
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
import java.util.Scanner;
public class StringBasic {
public static void main(String args[])
{
// Part 1: String concatenation
Scanner input = new Scanner(System.in);
System.out.print("Enter your first name: ");
String FirstName = input.nextLine();
System.out.print("Enter your middle name: ");
String MiddleName = input.nextLine();
System.out.print("Enter your last name: ");
String LastName = input.nextLine();
StringBuilder name = new StringBuilder();
name.append(FirstName);
name.append(" ");
name.append(MiddleName);
name.append(" ");
name.append(LastName);
String result = name.toString();
System.out.println(result);
// Part 2: String comparison
System.out.print("Enter another full name:");
String FullName = input.nextLine();
if(result.equalsIgnoreCase(FullName))
{
System.out.println("The names are the same.");
}
else
{
System.out.println("The names are different.");
}
// Part 3: String modification
for(int i = 0; i < result.length(); i++)
{
char ch = name.charAt(i);
if(ch == 'a')
{
name.deleteCharAt(i);
name.insert(i, '@');
}
else if(ch == 'e')
{
name.deleteCharAt(i);
name.insert(i, '3');
}
}
String ModifiedName = name.toString();
System.out.println(ModifiedName);
String UpperCase = result.toUpperCase();
System.out.println(UpperCase);
//Part 4: String Splitting
String[] nameParts = result.split(" ");
// Printing each part of the name
for (String part : nameParts) {
System.out.println(part);
}
// Part 5:
System.out.print("Enter a string with spaces at the beginning and the end: ");
String word = input.nextLine();
String TrimmedWord = word.trim();
System.out.println(TrimmedWord);
// Part 6: Additional Manipulations
int count = 0;
for(int i = 0; i < result.length(); i++)
{
char c = result.charAt(i);
if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
{
count++;
}
}
String message = String.format("The number of vowels in the string is: %d", count);
System.out.println(message);
input.close();
}
}