-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassignment1FirstWord.java
More file actions
31 lines (30 loc) · 1.01 KB
/
assignment1FirstWord.java
File metadata and controls
31 lines (30 loc) · 1.01 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
import java.util.Scanner;
public class assignment1FirstWord {
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
String wordA =scanner.nextLine();
String wordB =scanner.nextLine();
String result = FirstWord(wordA,wordB );
System.out.println(result);
}
static String FirstWord(String wordA,String wordB){
int minlength=Math.min(wordA.length(),wordB.length());
String result=" ";
boolean differenceFound=false;
for(int i=0;i<minlength;i++){
if (wordA.charAt(i)>wordB.charAt(i)) {
result = wordA;
differenceFound=true;
break;
} else if (wordA.charAt(i)<wordB.charAt(i)) {
result=wordB;
differenceFound=true;
break;
}
}
if(!differenceFound){
return (wordA.length()>wordB.length()) ? wordB :wordA;
}
return result;
}
}