-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFindWordsLength.java
More file actions
48 lines (38 loc) · 904 Bytes
/
Copy pathFindWordsLength.java
File metadata and controls
48 lines (38 loc) · 904 Bytes
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
package interviewQuestions;
import java.util.Arrays;
/**
* @author Simran
*
*/
public class FindWordsLength {
public static void main(String[] args) {
String str="this is java program";
// count(str);
// count1(str);
count2(str);
}
static void count(String str) {
char[] ch=str.toCharArray();
for (int i=0;i<ch.length;i++) {
StringBuilder s= new StringBuilder();
while(i<ch.length && ch[i]!=' ') {
s.append(ch[i]);
i++;
}
if(!s.isEmpty())
System.out.println(s+"->"+s.length());
}
}
static void count2(String str) {
String[] words = str.split(" ");
for (String s : words) {
if (!s.isEmpty()) {
System.out.println(s + " -> " + s.length());
}
}
}
static void count1(String str) {
Arrays.stream(str.split("\\s+"))
.forEach(word -> System.out.println(word + " -> " + word.length()));
}
}