-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStrings.java
More file actions
107 lines (96 loc) · 2.9 KB
/
Copy pathStrings.java
File metadata and controls
107 lines (96 loc) · 2.9 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
import java.util.*;
public class Strings {
public static boolean isPalindrome(String str){
for(int i=0; i<str.length()/2; i++){
int n = str.length();
if(str.charAt(i) != str.charAt(n-i-1)){
return false;
}
}
return true;
}
public static float getPath(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 == 'E'){
x++;
}
else{
x--;
}
}
int x2 = x*x;
int y2 = y*y;
return (float) Math.sqrt(x2+y2);
}
public static String substring(String str,int si, int ei){
String substr = "";
for(int i=si; i<=ei; i++){
substr += str.charAt(i);
}
return substr;
}
public static String Compress(String str){
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 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 void main(String[] args) {
// String str = "racecar";
// System.out.println(isPalindrome(str));
// String path = "WNEENESENNN";
// System.out.println(getPath(path));
// String str = "Hello";
// System.out.println(substring(str, 0, 2));
// 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("hello");
// for(char ch='a'; ch<='z'; ch++){
// sb.append(ch);
// }
// System.out.println(sb);
// String str = "aaabbbcccddd";
// System.out.println(Compress(str));
String str = "Hi i am jarvis";
System.out.println(toUpperCase(str));
}
}