-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrings.java
More file actions
124 lines (107 loc) · 3.47 KB
/
strings.java
File metadata and controls
124 lines (107 loc) · 3.47 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
import java.util.*;
public class strings {
public static void main (String args[]){
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
System.out.println(palindrome(str));
String name = "sakshi";
String fullname="sakshi sahu";
String names = sc.nextLine();
System.out.println("your name is "+ name);
System.out.println(fullname);
System.out.println(fullname.length());
// length of string
System.out.println(names.length());
// character
for(int i=0; i<fullname.length(); i++){
System.out.println(fullname.charAt(i));
}
String name1 = "sakshi";
String name2 = "sakshi";
// s1>s2 : +ve value
// s1 == s2 :0
// s1<s2 :-ve value
if(name1.compareTo(name2)==0){
System.out.println("string are equal");
}else{
System.out.println("strings are not equal");
}
if(new String("sakshi")== new String("Sakshi")){
System.out.println("strings are equals");
}else{
System.out.println("strings are not equals");
}
String sentences = "MY NAME IS SAKSHI";
String nam= sentences.substring(4, sentences.length());
System.out.println(nam);
StringBuilder sb = new StringBuilder("sak");
// char at index 0
System.out.println(sb.charAt(0));
// set char at index 0
sb.setCharAt(0, 't');
System.out.println(sb);
// insert function
sb.insert(2,'h');
System.out.println(sb);
//delete function
sb.delete(2, 3);
System.out.println(sb);
//append :last mai jodna
sb.append("q");
sb.append("u");
sb.append("e");
sb.append("e");
sb.append("n");
System.out.println(sb.length());
System.out.println(sb);
//reverse string
for(int i=0; i<sb.length()/2; i++){
int front =i;
int back = sb.length()-1-i;// 5-1-0 =>4
char frontchar = sb.charAt(front);
char backchar = sb.charAt(back);
//set char at -at any character
sb.setCharAt(front, backchar);
sb.setCharAt(back, frontchar);
}
// System.out.println(sb);
// String path = "W N E E N E S E N N N";
// System.out.println("THE SHORTEST PATH IS: "+ getShortPath(path));
}
// print a palindrome
public static boolean palindrome(String str){
for(int i=0; i<str.length()/2; i++){
int n= str.length();
if(str.charAt(i)!=str.charAt(n-1-i)){
// not a palindrome
return false;
}
}
return true;
}
//DIRECTION OF THE ROUTE FOR THE DISPLACEMENT
// public static float getShortPath(String path){
// float x=0,y=0;
// for(int i=0; i<=path.length(); i++){
// char direction = path.charAt(i);
// //south
// if(direction=='S'){
// y--;
// }
// //north
// else if(direction=='N'){
// y++;
// }
// //west
// else if(direction=='W'){
// x--;
// }
// //east
// else {
// y++;
// }
// float X2= x*x;
// float Y2 =y*y;
// }
// }
}