import java.util.Scanner; public class five { private String str1; private String str2; public five(String str1, String str2) { this.str1 = str1; this.str2 = str2; } public int usrstrcmp() { int len1 = str1.length(); int len2 = str2.length(); for (int i = 0; i < Math.min(len1, len2); i++) { if (str1.charAt(i) < str2.charAt(i)) { return -1; } else if (str1.charAt(i) > str2.charAt(i)) { return 1; } } if (len1 < len2) { return -1; } else if (len1 > len2) { return 1; } return 0; } public int usrstrcmp(int n) { if (n > str1.length() || n > str2.length()) { System.out.println("Invalid input: n is larger than string length."); return -2; } for (int i = 0; i < n; i++) { if (str1.charAt(i) < str2.charAt(i)) { return -1; } else if (str1.charAt(i) > str2.charAt(i)) { return 1; } } return 0; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter first string: "); String str1 = scanner.nextLine(); System.out.print("Enter second string: "); String str2 = scanner.nextLine(); System.out.print("Enter number of characters to compare: "); int n = scanner.nextInt(); five compare = new five(str1, str2); System.out.println("\nComparing full strings:"); int result1 = compare.usrstrcmp(); if (result1 == 0) { System.out.println("Strings are equal"); } else if (result1 == -1) { System.out.println("First string is smaller"); } else { System.out.println("First string is greater"); } System.out.println("\nComparing first " + n + " characters:"); int result2 = compare.usrstrcmp(n); if (result2 == 0) { System.out.println("First " + n + " characters are equal."); } else if (result2 == -1) { System.out.println("First " + n + " characters of first string are smaller."); } else if (result2 == 1) { System.out.println("First " + n + " characters of first string are greater."); } else { System.out.println("Error in comparison."); } scanner.close(); } }