Repository navigation
Expand file tree
/
Copy pathstrings.java
More file actions
203 lines (183 loc) · 5.84 KB
/
Copy pathstrings.java
File metadata and controls
203 lines (183 loc) · 5.84 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
//STRINGS - words sentences paragraphs
import java.util.Scanner;
// code 1
class Strings {
public static void printletters(String name) {
for (int i = 0; i < name.length(); i++) {
System.out.print(name.charAt(i) + " ");
}
}
public static void main(String[] args) {
char arr[] = { 'a', 'b', 'c', 'd' };
String str = "abcd";
String str2 = new String("xyz");
// STRINGS ARE IMMUTABLE
// Input and Output Strings
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
System.out.println(name);
// calculate the length of the length of the string
System.out.println(name.length());
// string concatenation
String firstname = "Priyanshi ";
String lastname = "Gupta";
String fullname = firstname + lastname;
System.out.println(fullname);
// access index at a certain index
System.out.println(name.charAt(0));
// print all letters
printletters(name);
}
}
// code 2 - check if the string is palindrome
class CheckPalindrome {
public static boolean checkpalin(String name) {
for (int i = 0; i < name.length() / 2; i++) {
if (name.charAt(i) != name.charAt(name.length() - i - 1)) {
return false;
}
}
return true;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
System.out.println(checkpalin(str));
}
}
// code 3 - SHORTEST PATH
class ShortestPath {
public static double calcshortpath(int x, int y) {
double dist = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
return dist;
}
public static double checkcoordinates(String s) {
int x = 0, y = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == 'N') {
y++;
} else if (s.charAt(i) == 'S') {
y--;
} else if (s.charAt(i) == 'E') {
x++;
} else if (s.charAt(i) == 'W') {
x--;
}
}
return calcshortpath(x, y);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the string of directions: ");
String str = sc.nextLine();
double result = checkcoordinates(str);
System.out.println("Shortest path distance: " + result);
}
}
// CODE 4 - COMPARE FUNCTION
class Compare {
public static void main(String[] args) {
String str = "Priyanshi";
String str2 = "Priyanshi";
String str3 = new String("Priyanshi");
if (str == str2) {
System.out.println("Strings are equal");
} else {
System.out.println("Strings are unequal");
}
if (str == str3) {
System.out.println("Strings are equal");
} else {
System.out.println("Strings are unequal");
}
if (str.equals(str2)) {
System.out.println("Strings are equal");
} else {
System.out.println("Strings are unequal");
}
if (str.equals(str3)) {
System.out.println("Strings are equal");
} else {
System.out.println("Strings are unequal");
}
}
}
// code 5 - STRING FUNCTIONS
class Functions {
public static void main(String[] args) {
String str = "Priyanshi";
// printing substring of a string
System.out.println(str.substring(0, 5));
// print largest string in lexicographic way ignoring the case - O(x*n)
String fruits[] = { "apple", "banana", "pineapple" };
String largest = fruits[0];
for (int i = 0; i < 3; i++) {
if (largest.compareToIgnoreCase(fruits[i]) > 0) {
largest = fruits[i];
}
}
System.out.println(largest);
}
}
// WHY ARE STRINGS IMMUTABLE - they follow interning (heap)- thus wastes a lot
// of time in loops
// code 6 - STRING BUILDER
class Builder {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("");
for (char ch = 'a'; ch <= 'z'; ch++) {
sb.append(ch); // way to add elements add the end of a string builder
}
System.out.println(sb);
// time complexity - O(26)
}
}
// code 7 - CONVERT LETTERS TO UPPERCASE
class Uppercase {
public static void Upper(String s) {
StringBuilder sb = new StringBuilder("");
// uppercase function
char ch = Character.toUpperCase(s.charAt(0));
sb.append(ch);
for (int i = 1; i < s.length(); i++) {
if (s.charAt(i) == ' ' && i < s.length() - 1) {
sb.append(s.charAt(i));
i++;
sb.append(Character.toUpperCase(s.charAt(i)));
} else {
sb.append(s.charAt(i));
}
}
System.out.println(sb);
}
public static void main(String[] args) {
System.out.print("Enter the string: ");
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
Upper(str);
}
}
// code 8 - STRING COMPRESSION
class StringCompression {
public static void Compresion(String s) {
String newstr = "";
for (int i = 0; i < s.length(); i++) {
int count = 1;
while (i < s.length() - 1 && s.charAt(i) == s.charAt(i + 1)) {
count++;
i++;
}
newstr += s.charAt(i);
if (count > 1) {
newstr += count;
}
}
System.out.println(newstr);
}
public static void main(String[] args) {
System.out.print("Enter the string: ");
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
Compresion(str);
}
}