-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyString2.java
More file actions
78 lines (58 loc) · 1.56 KB
/
MyString2.java
File metadata and controls
78 lines (58 loc) · 1.56 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
import java.util.Scanner;
class MyString2{
MyString2(){
}
public static int whichFirst(String str1, String str2){
int n = 0;
str1 = str1.toLowerCase();
str2 = str2.toLowerCase();
if(str1.compareTo(str2)<0)
n = 1;
else if(str1.compareTo(str2)>0)
n = 2;
else
n = 0;
return n;
}
public static String removeSpaces(String str){
String c = "";
for(int i = 0; i<str.length(); i++)
if(str.charAt(i) != ' ')
c += str.charAt(i);
return c;
}
public static String findLongestWord(String str){
int max = 0;
int count = 0;
int k = 0;
String s = "";
for(int i=0; i<str.length(); i++)
if(str.charAt(i) == ' '){
if(max < count){
max = count;
k = i-count;
}
count = 0;
continue;
}
else{
count++;
}
for(int i = k; ;i++){
if(str.charAt(i) == ' ')
break;
s += str.charAt(i);
}
return s;
}
public static void main(String[] args){
MyString2 l = new MyString2();
Scanner s = new Scanner(System.in);
String s1;
String s2;
s1 = s.nextLine();
s2 = l.findLongestWord(s1);
int n = s2.length();
System.out.printf("%s %d",s2,n);
}
}