-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrings.java
More file actions
171 lines (137 loc) · 4.89 KB
/
Copy pathstrings.java
File metadata and controls
171 lines (137 loc) · 4.89 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
import java.io.FileReader;
import java.util.Scanner;
import javax.annotation.processing.SupportedSourceVersion;
public class strings {
public static void printLetters(String fullName) {
for (int i = 0; i < fullName.length(); i++){
System.out.print(fullName.charAt(i) + " ");
}
System.out.println();
}
public static boolean isPallindrome(String pallindrome) {
for (int i = 0; i < pallindrome.length()/2; i++){
if (pallindrome.charAt(i) != pallindrome.charAt(pallindrome.length()-1-i)){
System.out.println("The word is not a pallindrome");
return false;
}
}
System.out.println(" The word is pallindrome");
return true;
}
public static float displacement(String path) {
int x = 0, y= 0;
for (int i = 0; i < path.length(); i++ ){
char dir = path.charAt(i);
if (dir == 'N'){
y--;
}else if (dir == 'S'){
y++;
}else if (dir == 'W'){
x--;
}else {
x++;
}
}
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) == ' ' && i < str.length()-1 ){
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 substr(String str, int si , int ei) {
String substring = "";
for (int i = si; i < ei; i++){
substring += str.charAt(i);
}
return substring;
}
public static String stringCompression(String str) {
String newstr = "";
for (int i = 0; i < str.length(); i++){ //O(n)
Integer ch = 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 void main(String[] args) {
// char arr[] = {'a', 'b', 'c', 'd'};
// String str = "abcd";
// String str2 = new String("xyz");
// Strings are IMMUTABLE
// System.out.println(str2);
//Taking input for a string
// Scanner sc = new Scanner(System.in);
// String name = sc.nextLine();
//String length
// System.out.println(name);
//concatination
// String firstName = "Bharat";
// String lastName = "Sheoran";
// String fullName = firstName + " " + lastName;
// printLetters(fullName);
// String pallindrome = "Racecar";
// isPallindrome(pallindrome);
// String path = "NNSSWWEE";
// System.out.println(displacement(path));\
// String s1 = "Bharat";
// String s2 = "Bharat";
// String s3 = new String("Bharat");
// if (s1 == s2){
// System.out.println("Strings are equal");
// } else{
// System.out.println("Strings are not equal");
// }
// if (s1 == s3){
// System.out.println("Strings are equal");
// } else{
// System.out.println("Strings are not equal");
// }
// if (s1.equals(s3)){
// System.out.println("Strings are equal");
// } else{
// System.out.println("Strings are not equal");
// }
// String str = "Bharat Sheoran";
// System.out.println(substr(str, 0, 6));
// Inbult function for substring
// System.out.println(str.substring(0 , 6));
// String fruits[] = {"apple", "mango", "banana"};
// String largest = fruits[0];
// for (int i = 0; i < fruits.length; i++ ){
// if (largest.compareTo(fruits[i]) < 0){
// largest = fruits[i];
// }
// }
// System.out.println(largest);
// StringBuilder sb = new StringBuilder("");
// for (char i = 'a'; i < 'z'; i++){
// sb.append(i);
// }
// System.out.println(sb);
// String str = "hi, i am bharat sheoran";
// System.out.println(toUpperCase(str));
String str = "abcd";
System.out.println(stringCompression(str));
}
}