Write a Java program that prompts the user to enter 3 string
Write a Java program that prompts the user to enter 3 strings, and displays the largest common prefix of the three strings. If there is a common prefix display the percentage the common prefix is of the longest string. For example if the largest string is 20 letters and the common prefix is 5 letters then the common prefix is 25% of the longest string. Do not include spaces in that determination. Display a message if there are no common prefixes.
I am having a bit of trouble getting the percent of the longest string to display, any ideas? This is what I have so far:
import java.util.Scanner; public class SecondProgram { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // Prompting user to enter 3 strings System.out.println(\"Enter the first string: \"); String first = sc.nextLine(); System.out.println(\"Enter the second string: \"); String second = sc.nextLine(); System.out.println(\"Enter the third string: \"); String third = sc.nextLine(); // Finding the longest common string int l = first.length(); int p = 0; float percent; if ( first.length() > second.length() && first.length() > third.length() ) l = first.length(); else if ( second.length() > first.length() && second.length() > third.length() ) l = second.length(); else if ( third.length() > first.length() && third.length() > second.length() ) l = third.length(); for (int i = 0; i <= l; i++) { if (first.charAt(i) != first.charAt(i)) { break; } if (first.charAt(i) != third.charAt(i)) { break; } p++; } // Printing common prefix if any if (p == 0) { System.out.println(first + \" and \" + second + \" and \" + third + \" have no common prefix. \"); } // Printing the percentage percent = (p / l) * 100; System.out.println(\"The common prefix is \" + first.substring(0, p)); System.out.println(\"The common prefix is\" + percent + \" the longest string. \"); if(first.length() > second.length()){ int perc = (first.substring(0, p).length() / second.length() ) *100 ; System.out.println(\"The common prefix is \" + perc + \"% of the longest string\"); } else{ int perc = (second.substring(0, p).length() / third.length() ) *100 ; System.out.println(\"The common prefix is \" + perc + \"% of the longest string\"); } } }
Solution
SecondProgram.java
import java.text.DecimalFormat;
import java.util.Scanner;
public class SecondProgram {
public static void main(String[] args) {
int k = 0;
//Decimal Format class object is used to format the output
DecimalFormat df=new DecimalFormat(\"#.##\");
//Scanner class object is used to read the inputs entered by the user
Scanner sc = new Scanner(System.in);
// Prompting user to enter 3 strings
System.out.println(\"Enter the first string: \");
String first = sc.nextLine();
System.out.println(\"Enter the second string: \");
String second = sc.nextLine();
System.out.println(\"Enter the third string: \");
String third = sc.nextLine();
// Finding the longest common string
int l = first.length();
int p = 0;
float percent;
//Finding longest string length
if (first.length() > second.length() && first.length() > third.length())
l = first.length();
else if (second.length() > first.length() && second.length() > third.length())
l = second.length();
else if (third.length() > first.length() && third.length() > second.length())
l = third.length();
//finding the shortest string length
if (first.length() < second.length() && first.length() < third.length())
k = first.length();
else if (second.length() < first.length() && second.length() < third.length())
k = second.length();
else if (third.length() < first.length() && third.length() < second.length())
k= third.length();
//Finding length of the common prefix
for (int i = 0; i < k; i++) {
if (first.charAt(i) != second.charAt(i)) {
break;
}
if (first.charAt(i) != third.charAt(i)) {
break;
}
p++;
}
// Printing the error message if there is no common prefix
if (p == 0) {
System.out.println(\"\'\"+first + \"\' and \'\" + second + \"\' and \'\" + third+ \"\' have no common prefix. \");
}
else
{
// calculating the common prefix percentage
percent = ((float)p / l) * 100;
//Displaying the common prefix
System.out.println(\"The common prefix is \'\" + first.substring(0, p)+\"\'\");
//Displaying the length of common prefix
System.out.println(\"The common prefix is \" + p+ \" letters . \");
//Displaying the percentage of common prefix of the longest string
System.out.println(\"The common prefix is \" + df.format(percent)+ \"% of the longest string\");
}
}
}
___________________________
Output1:
Enter the first string:
Hello Singh
Enter the second string:
Hello Roy
Enter the third string:
Hel Sam
The common prefix is \'Hel\'
The common prefix is 3 letters .
The common prefix is 27.27% of the longest string
_____________________________
Output2:
Enter the first string:
Hello how are you
Enter the second string:
Am Fine
Enter the third string:
Who are you
\'Hello how are you\' and \'Am Fine\' and \'Who are you\' have no common prefix.
_________________________________
Output3:
Enter the first string:
Hello
Enter the second string:
Hel
Enter the third string:
He
The common prefix is \'He\'
The common prefix is 2 letters .
The common prefix is 40% of the longest string
_______________________Thank You


