-
Notifications
You must be signed in to change notification settings - Fork 0
/
Strings.java
71 lines (59 loc) · 2.38 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
import java.util.*;
public class Strings {
public static void main(String[] args) {
// String s = "Tony";
// Scanner sc = new Scanner(System.in);
// String name = sc.nextLine();
// System.out.println("Your name is "+name);
// concatenation
// String firstName = "Ritwick";
// String lastName = "Pal";
// String fullName = firstName+" "+lastName;
// System.out.println(fullName);
// System.out.println(fullName.length());
// // System.out.println(fullName.charAt(0));
// for(int i=0; i<fullName.length(); i++){
// System.out.print(fullName.charAt(i) + " ");
// }
// string compare
// String name1 = "Tony";
// String name2 = "Tony";
// if (name1.equals(name2)) {
// System.out.println("They are the same string");
// } else {
// System.out.println("They are different strings");
// }
// // DO NOT USE == to check for string equality
// // Gives correct answer here
// if (name1 == name2) {
// System.out.println("They are the same string");
// } else {
// System.out.println("They are different strings");
// }
// // Gives incorrect answer here
// if (new String("Tony") == new String("Tony")) {
// System.out.println("They are the same string");
// } else {
// System.out.println("They are different strings");
// }
// String name = "RitwickPal";
// System.out.println(name.substring(0, 5));
// String str = "123";
// int number = Integer.parseInt(str); // string to integer conversion
// System.out.println(number);
// int number = 123;
// String str = Integer.toString(number);
// System.out.println(str);
// System.out.println(str.length());
// 1. Take an array of Strings input from the user & find the cumulative (combined) length of all those strings.
Scanner sc = new Scanner(System.in);
int size = sc.nextInt();
String array[] = new String[size];
int totLength = 0;
for (int i = 0; i < size; i++) {
array[i] = sc.next();
totLength += array[i].length();
}
System.out.println(totLength);
}
}