-
Notifications
You must be signed in to change notification settings - Fork 1
/
Strings.java
219 lines (182 loc) · 7.14 KB
/
Strings.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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import java.util.*;
public class Strings {
//Question 1:
public static boolean isPalindrome(String str) {
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) != str.charAt(str.length() - 1 - i)) {
return false;
}
}
return true;
}
//Question 2:
public static float getDisplacement(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.ceil(Math.sqrt(X2 + Y2));
}
//Question 3:
public static void lexicoGraphic(String[] str) {
String large = str[0];
for (int i = 1; i < str.length; i++) {
if (large.compareTo(str[i]) < 0) {
large = str[i];
}
}
System.out.println(large);
}
//Question 4:
public static String toCamelCase(String str) {
StringBuilder newstr = new StringBuilder("");
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == ' ' && i < str.length() - 1) {
newstr.append(str.charAt(i)); //added firstly character.
i++; //Increased the index after encountering white space.
newstr.append(Character.toUpperCase(str.charAt(i)));
} else {
newstr.append(str.charAt(i)); /*If there is NO Empty char then it'll join to newstr as it is.*/
}
}
return newstr.toString(); /*Finally converted the StringBuilder to string.*/
}
//Question 5:
public static String strCompression(String str) {
int n = str.length();
StringBuilder newstr = new StringBuilder("");
for (int i = 0; i < n; i++) {
Integer count = 1;
while (i < n - 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();
}
//Assignment Question 1:
public static void lowerCaseVOWELCount(String str) {
int count = 0;
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
count++;
}
}
System.out.println(count + " VOWELS in your string.");
}
public static int lowerCaseCount(String str) {
int count = 0;
for (int i = 0; i < str.length(); i++) {
if (Character.isLowerCase(str.charAt(i))) {
count++;
}
}
return count;
}
//Assignment Question 4:
public static boolean isAnagram(String str1, String str2) {
// Convert Strings to lowercase. Why? so that we don't have to check separately for lower & uppercase.
// str1 = str1.toLowerCase();
// str2 = str2.toLowerCase();
// First check - if the lengths are the same
if (str1.length() == str2.length()) {
// convert strings into char array
char[] str1charArray = str1.toCharArray();
char[] str2charArray = str2.toCharArray();
// sort the char array
Arrays.sort(str1charArray);
Arrays.sort(str2charArray);
// if the sorted char arrays are same or identical then the strings are anagram
boolean result = Arrays.equals(str1charArray, str2charArray);
if (result) {
System.out.println(str1 + " and " + str2 + " are anagrams of each other.");
return true;
} else {
System.out.println(str1 + " and " + str2 + " are not anagrams of each other.");
return false;
}
} else {
// case when lengths are not equal
System.out.println(str1 + " and " + str2 + " are not anagrams of each other.");
return false;
}
}
public static void main(String[] para_coder) {
Scanner scan = new Scanner(System.in);
//String declartion.
// String str1 = "sarvesh", str2 = new String("sarvesh");
// System.out.println("str1 = " + str1 + "\nstr2 = " + str2);
//next() Function.
// String fname = scan.next(); /*takes only single word.*/
// System.out.print(fname);
//nextLine() Function.
// String lname = scan.nextLine(); /*take string with white space.*/
// System.out.println(lname);
//String Length() Function.
// String stri = "I CAN AND I WILL";
// System.out.println("Lenght: " + stri.length());
//String Concatination.
// String fullName = "_" + "SARVESH" + "_";
// System.out.println("fullName = " + fullName);
//Question 1: Given string is palindrome or not.
// String str = scan.next();
// System.out.println(isPalindrome(str));
//Question 2: Find shortest path (displacement).
// String path = "WNEENESENN";
// System.out.println(getDisplacement(path));
//String Comparison
/*String s1 = "sad", s2 = "sad", s3 = new String("sad");
if (s1 == s2) System.out.println(1); //For String '==' equals to operator compares at object level.
else System.out.println(0);
if (s1.equals(s3)) System.out.println(1); //equal(obj) function compares objects.
else System.out.println(0);*/
//Substring
// String str = "sarvesh ashok devrukhakar";
// System.out.println(str.substring(8, 14));
//Question 3:
// String[] str = {"apple", "banana", "mango"};
// lexicoGraphic(str);
//Question 4:
// String str = " hi i am sarvseh.";
// System.out.println(toCamelCase(str));
//Question 5:
// String str = "abbcccdddd";
// System.out.println(strCompression(str));
//Assignment Question: 1
/*System.out.print("Enter String: ");
String str = scan.nextLine();
lowerCaseVOWELCount(str);*/
//Count Lower Case Character in your String.
/*System.out.print("Enter String: ");
String str = scan.nextLine();
System.out.println(lowerCaseCount(str) + " Lower Case Characters in your string.");*/
//Assignment Question: 2
// String str = "ShradhaDidi", str1 = "ApnaCollege", str2 = "ShradhaDidi";
// System.out.println(str.equals(str1) + " " + str.equals(str2));
//Assignment Question: 3
// String str = "ApnaCollege".replace("l", "");
// System.out.println(str);
//Assignment Question 4: Determine if 2 Strings are Anagrams of each other.
/*System.out.print("Enter String 1: ");
String str1 = scan.next();
System.out.print("Enter String 2: ");
String str2 = scan.next();
System.out.println(isAnagram(str1, str2));*/
}
}