-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path12. Strings.java
More file actions
190 lines (154 loc) · 5.66 KB
/
12. Strings.java
File metadata and controls
190 lines (154 loc) · 5.66 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
/*
- String is a Non-Primitive Datatype.
- Scanner is also a Non-Primitive Datatype in Java.
- sc.next() for printing a single word.
- To print a whole line/sentence use sc.nextLine().
- Strings are Inmutable.
- If we create a String in the memory, we cannot change that String. Not even delete, add, modify.
- If we want to modify or make any changes, have to create a new String.
*/
import java.util.*;
public class Strings {
public static void main(String[] args) {
// String Declaration
String name = "Aisha";
String fullName = "Aisha Pogo";
String sentence = "My name is Aisha Pogo.";
}
}
import java.util.*;
public class Strings {
public static void main(String[] args) {
// Taking Input
Scanner sc = new Scanner(System.in);
System.out.println("What is your name?");
String name = sc.nextLine();
System.out.print("Your name is: " + name);
}
}
/* Concatenation: In Java, concatenation refers to the operation of combining two or more strings into a single string.
This process is used to create a new string by appending one or more strings to the end of another string. */
import java.util.*;
public class Strings {
public static void main(String[] args) {
// concatenation
String firstName = "Aisha";
String lastName = "Pogo";
String fullName = firstName + " " + lastName;
System.out.println(fullName);
}
}
/* String Length Method: The length() method returns the length of a specified string.
It is a public member method and can be accessed using the dot operator (.). */
import java.util.*;
public class Strings {
public static void main(String[] args) {
// concatenation
String firstName = "Aisha";
String lastName = "Pogo";
String fullName = firstName + " " + lastName;
System.out.println(fullName.length());
}
}
/* charAt: The charAt() method returns the character at the specified index in a string. */
import java.util.*;
public class Strings {
public static void main(String[] args) {
// concatenation
String firstName = "Aisha";
String lastName = "Pogo";
String fullName = firstName + " " + lastName;
// charAt
for(int i=0; i<fullName.length(); i++) {
System.out.println(fullName.charAt(i));
}
}
}
/* compareTo: The compareTo() method compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings. */
import java.util.*;
public class Strings {
public static void main(String[] args) {
// compare
String name1 = "Aisha";
String name2 = "Aisha";
// case 1 : s1 > s2 => +ve value
// case 2 : s1 == s2 => 0
// case 3 : s1 < s2 => -ve value
if(name1.compareTo(name2) == 0) {
System.out.println("Strings are equal.");
} else {
System.out.println("Strings are not equal.")
}
}
}
/* substring: A substring is a part of a larger string, extracted using the substring() method of the String class.
This method returns a new string that is a subset of the original string, starting from a specified index (inclusive) and ending at another specified index (exclusive). */
import java.util.*;
public class Strings {
public static void main(String[] args) {
// substring
String sentence = "My name is Aisha";
// substring(beg index, end index)
String name = sentence.substring(11, sentence.length());
System.out.println(name);
}
}
/* Q1. Take an array of Strings input from the user & find the cumulative (combined) length of all those strings. */
import java.util.*;
public class Strings {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter array size: ");
int size = sc.nextInt();
String array[] = new String[size];
int totLength = 0;
System.out.println("Enter inputs: ");
for(int i=0; i<size; i++) {
array[i] = sc.next();
totLength += array[i].length();
}
System.out.print("Combined Length: " + totLength);
}
}
/* Q2. Input a string from the user. Create a new string called ‘result’ in which you will replace the letter "e" in the original string with letter "i".
Example: original = "eabcdef" ; result = "iabcdif"
Original = "xyz" ; result = "xyz"
*/
import java.util.*;
public class Strings {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the word: ");
String str = sc.next();
String result = "";
for(int i=0; i<str.length(); i++) {
if(str.charAt(i) == 'e') {
result += 'i';
} else {
result += str.charAt(i);
}
}
System.out.println("Result: " + result);
}
}
/* Q3. Input an email from the user. You have to create a username from the email by deleting the part that comes after ‘@’. Display that username to the user.
Example:
email = "helloWorld123@gmail.com"; username = "helloWorld123"
*/
import java.util.*;
public class Strings {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the email: ");
String email = sc.next();
String userName = "";
for(int i=0; i<email.length(); i++) {
if(email.charAt(i) == '@') {
break;
} else {
userName += email.charAt(i);
}
}
System.out.println("Username is: " + userName);
}
}