-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathques1.java
More file actions
35 lines (30 loc) · 1.09 KB
/
ques1.java
File metadata and controls
35 lines (30 loc) · 1.09 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
import java.util.*;
public class ques1 {
public static int compare(String string1, String string2){
for(int i = 0; i < string1.length() && i < string2.length(); i++){ //iterate in the strings
if((int)string1.charAt(i) == (int)string2.charAt(i)){
continue;
}
else{
if((int)string1.charAt(i) > (int)string2.charAt(i)) return 1;//return 1 if greater
else return -1;
}
}
if(string1.length() == string2.length()){
return 0;
}if(string1.length() > string2.length()){
return 1;
}if(string1.length() < string2.length()){
return -1;
}
return 0;
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("Enter First String: ");
String string1 = sc.nextLine();
System.out.println("Enter Second String: ");
String string2 = sc.nextLine();
System.out.println(compare(string1, string2));
}
}