From f396664d75effc06222af0a138c77b7eb9dc58bd Mon Sep 17 00:00:00 2001 From: Shivaraj Shetty Date: Mon, 7 Oct 2024 11:54:35 +0530 Subject: [PATCH] Added Anagram oftwo strings --- .gitignore | 3 ++- Java/Anagram.java | 56 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 Java/Anagram.java diff --git a/.gitignore b/.gitignore index dbe9c82..2f88d56 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -.vscode/ \ No newline at end of file +.vscode/ +*.class \ No newline at end of file diff --git a/Java/Anagram.java b/Java/Anagram.java new file mode 100644 index 0000000..9297bc4 --- /dev/null +++ b/Java/Anagram.java @@ -0,0 +1,56 @@ +// # Anagram + +// ## Problem Statement + +// Write a program that takes two strings as input and determines if they are anagrams of each other. An anagram is a word or phrase formed by rearranging the letters of another. + +// Your program should print "Anagram" if the two input strings are anagrams and "Not Anagram" if they are not. + +// ## Input + +// - Two strings, `str1` and `str2`, consisting of lowercase or uppercase letters and spaces. + +// ## Output + +// - "Anagram" if the two input strings are anagrams. +// - "Not Anagram" if the two input strings are not anagrams. + +// ## Example + +// #### Input +// str1 = "listen" +// str2 = "silent" + +// #### Output +// Anagram + + + + +import java.util.*; + +public class Anagram{ + public static void main(String args[]) + { + Scanner sc = new Scanner(System.in); + String s1,s2; + System.out.println("Enter string 1"); + s1=sc.next(); + System.out.println("Enter string 2"); + s2=sc.next(); + + char arr1[] = s1.toCharArray(); + char arr2[] = s2.toCharArray(); + + Arrays.sort(arr1); + Arrays.sort(arr2); + + boolean res = Arrays.equals(arr1,arr2); + + if(res==true) + System.out.println("Anagram"); + else + System.out.println("Not Anagram"); + + } +} \ No newline at end of file