Write a Program to check if two strings are Anagram or not

27 June 2022

Write a Program to check if two strings are Anagram or not


If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023

Description

Get two strings as input from the user and check whether it is Anagram or not.

 

Input

sunlight

thgiluns

 

Output

Anagram

C Program

#include<iostream>

#include<string.h>

using namespace std;

int main()

{

    char str1[100],str2[100];

    int f[26]={0}, s[26]={0}, c=0, flag=0;

    cout<<"Enter first string: ";

    cin>>str1;

    cout<<"Enter second string: ";

    scanf("%s",str2);

    while(str1[c] != '\0')

    {

        f[str1[c]-'a']++;

        c++;

    }

    c=0;

    while(str2[c] != '\0')

    {

        s[str2[c]-'a']++;

        c++;

    }

    for(c=0;c<26;c++)

    {

        if(f[c] != s[c])

            flag=1;

    }

    if(flag == 0)

    {

        printf("Anagram.");

    }

    else

    {

        printf("Not Anagram.");

    }

    return 0;

 

}

 

C++ Program

#include<iostream>

#include<string.h>

using namespace std;

int main()

{

    char str1[100],str2[100];

    int f[26]={0}, s[26]={0}, c=0, flag=0;

    cout<<"Enter first string: ";

    cin>>str1;

    cout<<"Enter second string: ";

    scanf("%s",str2);

    while(str1[c] != '\0')

    {

        f[str1[c]-'a']++;

        c++;

    }

    c=0;

    while(str2[c] != '\0')

    {

        s[str2[c]-'a']++;

        c++;

    }

    for(c=0;c<26;c++)

    {

        if(f[c] != s[c])

            flag=1;

    }

    if(flag == 0)

    {

        printf("Anagram.");

    }

    else

    {

        printf("Not Anagram.");

    }

    return 0;

 

}

 

Java

import java.util.Scanner;

import java.util.Arrays;

public class Main

{

                             static boolean isAnagram(String str1 , String str2) {

    String s1 = str1.replaceAll("[\\s]", "");

    String s2 = str2.replaceAll("[\\s]", "");

    boolean stat=true;

 

     if(s1.length()!=s2.length())

         stat = false;

     else {

         char[] arr1 = s1.toLowerCase().toCharArray();

         char[] arr2 = s2.toLowerCase().toCharArray();

         Arrays.sort(arr1);

         Arrays.sort(arr2);

         stat = Arrays.equals(arr1, arr2);

       }

       return stat;

}

   public static void main(String[] args) {

     Scanner sc = new Scanner(System.in);

     System.out.print("Enter two string: ");

     String str1 = sc.next();

     String str2 = sc.next();

     boolean stat = isAnagram(str1,str2);

       if(stat)

          System.out.println("Anagram");

       else

          System.out.println("Not Anagram");

       }

             

}

 

 

Python

Str1 = input('Enter first string: ')

Str2 = input('Enter second string: ')

if len(Str1) != len(Str2):

    print('Not anagram')

else:

    Str1 = sorted(Str1)

    Str2 = sorted(Str2)

    if Str1 == Str2:

        print('Anagram')

    else:

         print('Not anagram')


If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023
Related Articles

Ask Us Anything !