0% found this document useful (0 votes)
18 views2 pages

Anagram Program

The Java program checks if two input words are anagrams of each other by comparing the frequency of each character in both words. It ignores spaces and is case-insensitive. If the character counts match, it outputs 'Anagram'; otherwise, it outputs 'Not anagram'.

Uploaded by

unknown 1593
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views2 pages

Anagram Program

The Java program checks if two input words are anagrams of each other by comparing the frequency of each character in both words. It ignores spaces and is case-insensitive. If the character counts match, it outputs 'Anagram'; otherwise, it outputs 'Not anagram'.

Uploaded by

unknown 1593
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

import java.util.

*;
class Anagram
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
System.out.println("Enter two words:");
String w1=in.nextLine();
String w2=in.nextLine();
String w3="";
String w4="";
for(int r=0;r<w1.length();r++)
{
if(w1.charAt(r)!=' ')
{
w3=w3+w1.charAt(r);
}
}
for(int r=0;r<w2.length();r++)
{
if(w2.charAt(r)!=' ')
{
w4=w4+w2.charAt(r);
}
}
w3=w3.toLowerCase();
w4=w4.toLowerCase();
int c1=0,c2=0;
int f=1;
for(char i='a';i<='z';i++)
{
for(int j=0;j<w3.length();j++)
{
if(i==w3.charAt(j))
{
c1++;
}
}
for(int k=0;k<w4.length();k++)
{
if(i==w4.charAt(k))
{
c2++;
}
}
if(c1==c2)
{
c1=0;
c2=0;
continue;
}
else
{
f=0;
break;
}
}
if(f==1)
{
System.out.println("Anagram");
}
else
{
System.out.println("Not anagram");
}
}
}

You might also like