/* Java Program Example - Check Anagram or Not */
import java.util.Scanner;
public class JavaProgramANAGRAM1
{
public static void main(String[] input)
{
String str1, str2;
int len1, len2, i, j, found=0, not_found=0;
Scanner scan = new Scanner(System.in);
System.out.print("Enter First String : ");
str1 = scan.nextLine();
System.out.print("Enter Second String : ");
str2 = scan.nextLine();
len1 = str1.length();
len2 = str2.length();
if(len1 == len2)
{
for(i=0; i<len1; i++)
{
// found = 0;
for(j=0; j<len1; j++)
{
if(str1.charAt(i) == str2.charAt(j))
{
found++;
break;
}
}
}
if(found == len1)
{
System.out.print("Strings are Anagram to Each Other..!!");
}
else
{
System.out.print("Strings are not Anagram to Each Other..!!");
}
}
else
{
System.out.print("Both Strings Must have the same number of Character to be an Anagram");
}
}
}
Output :
import java.util.Scanner;
public class JavaProgramANAGRAM1
{
public static void main(String[] input)
{
String str1, str2;
int len1, len2, i, j, found=0, not_found=0;
Scanner scan = new Scanner(System.in);
System.out.print("Enter First String : ");
str1 = scan.nextLine();
System.out.print("Enter Second String : ");
str2 = scan.nextLine();
len1 = str1.length();
len2 = str2.length();
if(len1 == len2)
{
for(i=0; i<len1; i++)
{
// found = 0;
for(j=0; j<len1; j++)
{
if(str1.charAt(i) == str2.charAt(j))
{
found++;
break;
}
}
}
if(found == len1)
{
System.out.print("Strings are Anagram to Each Other..!!");
}
else
{
System.out.print("Strings are not Anagram to Each Other..!!");
}
}
else
{
System.out.print("Both Strings Must have the same number of Character to be an Anagram");
}
}
}
Output :
No comments:
Post a Comment