The program takes two input strings from the user, and then it checks whether they are anagrams or not using the is_anagrams function. The is_anagrams function checks whether the length of both strings is the same, and if they are not, it returns false. If the lengths are equal, it converts both strings to character arrays and sorts them using the Arrays.sort function. Then it converts the sorted character arrays back to strings and checks whether they are equal using the equals function.
package com.includehelp.stringsample; import java.util.Arrays; import java.util.Scanner; public class Anagram_Not { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter the String 1 : "); String str1 = input.next(); System.out.print("Enter the String 2 : "); String str2 = input.next(); if(is_anagrams(str1,str2)) { System.out.println("Anagram Strings"); } else { System.out.println("Strings are not Anagram !!"); } } static boolean is_anagrams(String str1,String str2) { if(str1.length()!=str2.length()) { return false; } char[] str_arr1 = str1.toCharArray(); char[] str_arr2 = str2.toCharArray(); Arrays.sort(str_arr1); Arrays.sort(str_arr2); String sort_s1 = new String(str_arr1); String sort_s2 = new String(str_arr2); if(sort_s1.equals(sort_s2)) { return true; } else { return false; } } }
Enter the String 1 : joes Enter the String 2 : oejs Anagram Strings
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions