Q:

(Longest common prefix) Write a program that prompts the user to enter two strings and displays the largest common prefix of the two strings. Here are some sample runs:

0

(Longest common prefix) Write a program that prompts the user to enter two strings and displays the largest common prefix of the two strings. Here are some sample runs:

Output:

Enter the first string: Welcome to C++
Enter the second string: Welcome to programming
The common prefix is Welcome to

Enter the first string: Atlanta
Enter the second string: Macon
Atlanta and Macon have no common prefix

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

/*
(Longest common prefix) Write a program that prompts the user to enter two
strings and displays the largest common prefix of the two strings.
*/
import java.util.Scanner;

public class Exercise_05_51 {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);

		// Prompt the user to enter two strings
		System.out.print("Enter the first string: ");
		String string1 = input.nextLine();
		System.out.print("Enter the second string: ");
		String string2 = input.nextLine();

		int index = 0;				// Initialize index to 0
		String prefix = "";		// Initialize prefix as empty string

		// Get the largest commmon prefix of the two strings
		while (string1.charAt(index) == string2.charAt(index)) {
			prefix += string1.charAt(index);
			index++;
		}

		// Display the result
		if (prefix.length() > 0)
			System.out.println("The commmon prefix is " + prefix);
		else
			System.out.println(string1 + " and " + string2 +
				" have no commmon prefix");
	}
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now