Program to check two strings are anagrams or not

Problem statement:-  Program to check two strings are anagrams or not

 Data requirement:-

   Input Data:- str

  Output Data:- str or Final_String (In Python)

  Additional Data:-i, j or str2 in Python

Program in C

Here is the source code of the C Program to check two strings are anagrams or not.

Code:

#include<stdio.h>
#include<string.h>
void sort(char str[30])
{
    char temp;
    int i,j;
    for(i=0;i<strlen(str)-1;i++)
    {
        for(j=i+1;j<strlen(str);j++)
        {
            if(str[i]>str[j])
            {
                temp=str[i];
                str[i]=str[j];
                str[j]=temp;
            }
        }
    }
}

int main()
{
    char str[30];
    printf("Enter your 1st String:");
    gets(str);
    char str2[30];
    printf("Enter your 2nd String:");
    gets(str2);
    if(strlen(str)!=strlen(str2))
    printf("Strings are not an Anagrams.");
    else
    {
    int i,count=0;

    //Sort two string
    sort(str);
    sort(str2);

    for(i=0;i<strlen(str);i++)
    {

        if(str[i]!=str2[i]){
            count=1;
    }}
    if(count==1)
    {
        printf("Input strings are not an anagram");
    }
    else
        printf("Input strings are an anagram");
}}

Input/Output:
Enter your 1st String:abaa
Enter your 2nd String:aaba
Input strings are an anagram

Program in C++

Here is the source code of the C++ Program to check two strings are anagrams or not.

Code:

#include<iostream>
using namespace std;
string sort_s(string str3,int len)
{
    char temp;
    int i,j;
    for(i=0;i<len-1;i++)
    {
        for(j=i+1;j<len;j++)
        {
            if(str3[i]>str3[j])
            {
                temp=str3[i];
                str3[i]=str3[j];
                str3[j]=temp;
            }
        }
    }
    return str3;
}

int main()
{
    string str,str2;
    cout<<"Enter your 1st String:";
    getline(cin,str);

    cout<<"Enter your 2nd String:";
    getline(cin,str2);

    int len=0,len2=0,i;
    while(str[len]!='\0')
    {
        len++;
    }

    while(str2[len2]!='\0')
    {
        len2++;
    }

    if(len!=len2)
    cout<<"Strings are not an Anagrams.";
    else
    {
    int i,count=0;

    //Sort two string
    string str4=sort_s(str,len);
    string str5=sort_s(str2,len);

    for(i=0;i<len;i++)
    {

        if(str4[i]!=str5[i]){
            count=1;
    }}
    if(count==1)
    {
        cout<<"Input strings are not an anagram";
    }
    else
        cout<<"Input strings are an anagram";
}}

Input/Output:
Enter your 1st String:cat
Enter your 2nd String:rat
Input strings are not an anagram

Program in Java

Here is the source code of the Java Program to check two strings are anagrams or not.

Code:

import java.util.Scanner;
public class p30 {
static String sort(String str)
{
    char temp;
    int i,j;
    String str2="";
    char[] str1=str.toCharArray();
    for(i=0;i<str.length()-1;i++)
    {
        for(j=i+1;j<str.length();j++)
        {   
            if(str1[i]>str1[j])
            {
                temp=str1[i];
                str1[i]=str1[j];
                str1[j]=temp;
            }
        }
  
    }
    for(i=0;i<str.length();i++)
    str2+=str1[i];
    return str2; 
}
public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
String str1;
System.out.println("Enter your 1st String:");
str1=cs.nextLine();
String str2;
System.out.println("Enter your 2nd String:");
str2=cs.nextLine();
if(str1.length()!=str2.length())
System.out.println("Strings are not an Anagrams.");
    else
    {
    int i,count=0;

    //Sort two string
    String str4=sort(str1);
    String str5=sort(str2);
    
    char[] str_1=str4.toCharArray();
    char[] str_2=str5.toCharArray();
    
    for(i=0;i<str1.length();i++)
    {

        if(str_1[i]!=str_2[i]){
            count=1;
    }}
    if(count==1)
        System.out.println("Input strings are not an anagram");
    else
    System.out.println("Input strings are an anagram");
}
cs.close();
}
}

Input/Output:
Enter your 1st String:
tar
Enter your 2nd String:
rat
Input strings are an anagram

Program in Python

Here is the source code of the Python Program to check two strings are anagrams or not.

Code:


str=input("Enter the 1st String:")
str2=input("Enter the 2nd String:")
if len(str) != len(str2):
    print("Strings are not an Anagrams.")
else:
    count = 0
    str3=''.join(sorted(str))
    str4=''.join(sorted(str2))
    for i in range(len(str)):
        if str3[i] != str4[i]:
            count=1
    if count == 1:
        print("Input strings are not an anagram")
    else:
        print("Input strings are an anagram")


Input/Output:
Enter the 1st String:accb
Enter the 2nd String:bcav
Input strings are not an anagram

More:-

C/C++/Java/Python Practice Question 


Post a Comment

0 Comments