Wednesday, July 1, 2020

Anagram program in C

Anagram program in JAVA


Anagram program in C to check whether two strings are anagrams or not. The strings are assumed to contain only lower case letters. The strings are anagrams of each other if the letters of one string can be rearranged to form the other string. So, in anagram strings, all characters occur the same number of times. For example, "ABC" and "CAB" are anagrams, as every character 'A,' 'B,' and 'C' occur the same number of times (one time here) in both the strings.
PROGRAM:
import java.util.Arrays;
public class anagram {
    
    public static void main(String args[])
    {
        String s1= new String("creative");
        String s2= new String("reactive");
        
        char temp1[]=s1.toCharArray();
        char temp2[]=s2.toCharArray();
        Arrays.sort(temp1);
        
        Arrays.sort(temp2);
       // System.out.println(""+temp1 +"" +temp2);
       
           if(Arrays.equals(temp1,temp2))
           {
               System.out.println("Anagram");
               
           }
        
        else
        {
            System.out.println("Not Anagram");
            
        }
        }
    }
OUTPUT:
Anagram

No comments:

Post a Comment