Untitled
unknown
plain_text
8 months ago
1.5 kB
8
Indexable
Never
import java.util.Scanner; import java.util.Arrays; public class StringOperations { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Input two strings System.out.print("Enter the first string: "); String str1 = scanner.nextLine(); System.out.print("Enter the second string: "); String str2 = scanner.nextLine(); // a. Sort strings in alphabetical order String sortedStr1 = sortString(str1); String sortedStr2 = sortString(str2); System.out.println("Sorted Strings:"); System.out.println("String 1: " + sortedStr1); System.out.println("String 2: " + sortedStr2); // b. Check if one string is a substring of another boolean isSubstring = str1.contains(str2) || str2.contains(str1); System.out.println("Is one string a substring of another? " + isSubstring); // c. Convert strings to uppercase String upperCaseStr1 = str1.toUpperCase(); String upperCaseStr2 = str2.toUpperCase(); System.out.println("Uppercase Strings:"); System.out.println("String 1: " + upperCaseStr1); System.out.println("String 2: " + upperCaseStr2); scanner.close(); } // Helper method to sort a string alphabetically private static String sortString(String str) { char[] charArray = str.toCharArray(); Arrays.sort(charArray); return new String(charArray); } }
Leave a Comment