-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestPrefix.java
More file actions
24 lines (24 loc) · 822 Bytes
/
LongestPrefix.java
File metadata and controls
24 lines (24 loc) · 822 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
package com.example;
import java.util.Scanner;
public class LongestPrefix {
public static void main(String args[]){
Scanner input = new Scanner(System.in);
System.out.print("Enter a first string : ");
String str1 = input.next();
System.out.println("------------------------------");
System.out.print("Enter a second string : ");
String str2 = input.nextLine();
System.out.println(LongestPrefix(str1,str2));
}
public static String LongestPrefix(String str1, String str2){
boolean firstComp = str1.charAt(0) == str2.charAt(0);
// if(!firstComp)
// return ;
String concate = " " + str1.charAt(0);
for(int i = 1 ; i < Math.min(str1.length(),str2.length()) && firstComp ; i++){
if(str1.charAt(i) == str2.charAt(i))
concate = concate + str1.charAt(i);
}
return concate;
}
}