Program to find strings are "anagram" or not

                                 This is a program which uses java concept to find out the given strings are anagram or not. Well, anagram is a concept in which two strings will be having same alphabets but the spelling would be different. This program uses String class to declare objects and the methods used are accordingly to check the strings. Look at the sample output and the code given below.
Sample Output:
Program Code:

import java.util.*;
class AnagramEx
{
 public static void main(String[] args)throws Exception
 {
  Scanner sc=new Scanner(System.in);
  System.out.println("Enter first string:");
  String s1=sc.nextLine();
  System.out.println("Enter second string:");
  String s2=sc.nextLine();
  Boolean flag=true;
  int i,j;
  i=s1.length();
  j=s2.length();
  if(i==j)
  {
   for (int k=0;k<i ;k++ )
   {
    for(int l=0;l<i;l++)
    {
     if(s1.charAt(k)==s2.charAt(l))
     {
      flag=true;
      break;
     }
     else
     {
      flag=false;
     }
     }
    }
   }
   else
   {
    flag=false;
   }
   if(flag==true)
    System.out.println("Strings are anagrams");
   else
    System.out.println("Strings are not anagrams");
  
 }
}

                             This is a simple code, which counts the number of words first, then if no.of words are equal, then it goes further to check alphabet by alphabet and if all the alphabets are equal, then the strings are anagram. I hope you have understood the concept. If you have any query, then comment below,or contact me directly.

Comments

Popular posts from this blog

Non Restoring Division Algorithm Implementation in C

Bit Stuffing Code Implementation in Java

Hackerrank Modified Kaprekar Numbers Solution