-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStrings.java
More file actions
163 lines (142 loc) · 4.58 KB
/
Copy pathStrings.java
File metadata and controls
163 lines (142 loc) · 4.58 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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import java.util.Scanner;
public class Strings {
public static void printLetters(String str) {
for (int i = 0; i < str.length(); i++) {
System.out.print(str.charAt(i) + " ");
}
System.out.println();
}
public static boolean isPalindrome(String str) {
for (int i = 0; i < str.length() / 2; i++) {
if (str.charAt(i) != str.charAt(str.length() - 1 - i)) {
return false;
}
}
return true;
}
public static float Distance(String str) {
int x = 0, y = 0;
for (int i = 0; i < str.length(); i++) {
switch (str.charAt(i)) {
case 'N': // north
y++;
break;
case 'E': // east
x++;
break;
case 'W': // west
x--;
break;
case 'S': // south
y--;
break;
}
}
int x2 = x * x;
int y2 = y * y;
return (float) Math.sqrt(x2 + y2);
}
public static String toUpperCase(String str) {
StringBuilder sb = new StringBuilder("");
char ch = Character.toUpperCase(str.charAt(0));
sb.append(ch);
for (int i = 1; i < str.length(); i++) {
if (str.charAt(i) == ' ') {
sb.append(str.charAt(i));
i++;
sb.append(Character.toUpperCase(str.charAt(i)));
} else {
sb.append(str.charAt(i));
}
}
return sb.toString();
}
public static String compress(String str) { // aaabbcccdd
String newStr = "";
for (int i = 0; i < str.length(); i++) {
Integer count = 1;
while (i < str.length() - 1 && str.charAt(i) == str.charAt(i + 1)) {
count++;
i++;
}
newStr += str.charAt(i);
if (count > 1) {
newStr += count.toString();
}
}
return newStr;
}
public static String compress2(String str) { // aaabbcccdd
StringBuilder newStr = new StringBuilder("");
for (int i = 0; i < str.length(); i++) {
Integer count = 1;
while (i < str.length() - 1 && str.charAt(i) == str.charAt(i + 1)) {
count++;
i++;
}
newStr.append(str.charAt(i));
if (count > 1) {
newStr.append(count.toString());
}
}
return newStr.toString();
}
public static void main(String args[]) {
char arr[] = { 'a', 'b', 'c', 'd' };
// defining string
String str = "Aditya";
String str2 = new String("Srivastava");
Scanner sc = new Scanner(System.in);
String name;
// entire line as input
name = sc.nextLine();
System.out.print("Size of " + name + " is ");
// length
System.out.println(name.length());
sc.close();
// concatenation
String FullName = str + " " + str2;
System.out.println(FullName);
printLetters(FullName);
System.out.println(isPalindrome(name));
String Distance = "WNEENESENNN";
System.out.println(Distance(Distance));
// .equals()
System.out.println("************** .equals() function **************");
String str3 = "Tony";
String str4 = "Tony";
String str5 = new String("Tony");
if (str3 == str4) {
System.out.println("Equal");
} else {
System.out.println("Not Equal");
}
if (str3 == str5) {
System.out.println("Equal");
} else {
System.out.println("Not Equal");
}
if (str3.equals(str5)) {
System.out.println("Equal");
} else {
System.out.println("Not Equal");
}
String fruits[] = { "apple", "mango", "banana" };
String largest = fruits[0];
for (int i = 1; i < fruits.length; i++) {
if (largest.compareTo(fruits[i]) < 0) {
largest = fruits[i];
}
}
System.out.println(largest);
StringBuilder sb = new StringBuilder("");
for (char ch = 'a'; ch <= 'z'; ch++) { // time complexity = O(26)
sb.append(ch);
}
System.out.println(sb);
String str6 = "hi, i am aditya";
System.out.println(toUpperCase(str6));
String str7 = "aaabbcccdd";
System.out.println(compress2(str7));
}
}