EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Anagram Program in C

Home » Software Development » Software Development Tutorials » C Programming Tutorial » Anagram Program in C

Anagram-Program-in-C

Introduction to Anagram Program in C

Two strings are said to be anagrams of each other if one string can be converted to form another string by rearranging the letters of one string and the number of characters in both the strings must be the same.

Consider two strings “abc” and “cab” for example, the occurrence of the characters a, b and c are same in both the strings and when all the characters in the string “abc” are rearranged, the string “cab” can be formed, hence the strings “abc” and “cab” are anagrams. Whenever two strings are given as input by the user, the frequency of each letter is calculated. The frequency of a letter is the number of occurrences of the letter in the string.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Algorithm

Algorithm to find whether the given two Strings are Anagram or Not?

  • Step 1: Two strings must be defined.
  • Step 2: Find out the length of each string. The strings are not anagrams if the length of one string is not equal to the length of other string.
  • Step 3: If the lengths of the two strings are equal, the characters in the string must be converted to lower case letters. We do this conversion in order to make a comparison between the two strings easier.
  • Step 4: The next step is to sort the characters in the strings. In some of the programming languages, inbuilt functions are available for sorting of strings. If there are no inbuilt functions to sort the strings, convert the strings to a character array.
  • Step 5: The strings converted to character array must be sorted.
  • Step 6: Finally, the content is checked for equality.

Examples of Anagram Program in C

Given below are the examples of Anagram Program in C:

Example #1

  • C Program to find if the given two strings are anagrams or not using the sorting method.

In this program, both the strings are sorted and then compared, after comparing the lengths of the strings.

Code:

#include <stdio.h>
#include <string.h>
//Declare the function names that are defined in the program later
void converttoLowercase(char[]);
void Arraysort(char[]);
int main ()
{
char string1[] = "Rat", string2[] = "Tar";
int a1, b = 0;
//length of strings is compared
if(strlen(string1) != strlen(string2))
{
printf("Both the strings are not anagram");
return 0;
}
else
{
//the strings are converted to lowercase
converttoLowercase(string1);
converttoLowercase(string2);
//The arrays are sorted by calling the function Arraysort()
Arraysort(string1);
Arraysort(string2);
for(a1 = 0; a1 < strlen(string1); a1++)
{
if(string1[a1] != string2[a1])
{
printf("Both the strings are not anagram");
return 0;
}
}
printf("Both the strings are anagram");
}
return 0;
}
void converttoLowercase(char a[])
{
int c;
for(c = 0; c < strlen(a)-1; c++)
{
a[c] = a[c]+32;
}
}
void Arraysort(char a[])
{
int temperory = 0,k,l;
for(k = 0; k < strlen(a)-1; k++)
{
for (l = k+1; l < strlen(a); l++)
{
if(a[k] > a[l])
{
temperory = a[k];
a[k] = a[l];
a[l] = temperory;
}
}
}
}

Popular Course in this category
C Programming Training (3 Courses, 5 Project)3 Online Courses | 5 Hands-on Projects | 34+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,570 ratings)
Course Price

View Course

Related Courses
C++ Training (4 Courses, 5 Projects, 4 Quizzes)Java Training (40 Courses, 29 Projects, 4 Quizzes)

Output:

sorting method

Example #2

  • C Program to find if the given two strings are anagrams or not using nested for loops.

In this program, the lengths of the strings are calculated and then compared inside nested for loops.

Code:

#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char string1[20], string2[20];
int leng, leng1, leng2, a, b, found1=0, not_found1=0;
printf("first string must be entered: ");
gets(string1);
printf("second string must be entered: ");
gets(string2);
//length of the first string is calculated
leng1 = strlen(string1);
//length of the first string is calculated
leng2 = strlen(string2);
//compare the length of the two strings to find out if the strings are anagram or not
if(leng1 == leng2)
{
leng = leng1;
for(a=0; a<leng; a++)
{
found1 = 0;
for(b=0; b<leng; b++)
{
if(string1[a] == string2[b])
{
found1 = 1;
break;
}
}
if(found1 == 0)
{
not_found1 = 1;
break;
}
}
if(not_found1 == 1)
printf("\nThe two entered strings are not Anagram");
else
printf("\nThe two entered strings are Anagram");
}
else
printf("\nsame number of characters must be present in both the strings to be an Anagram");
getch();
return 0;
}

Output:

Anagram program in c 2

Example #3

  • C Program to find if the given two strings are anagrams or not by calculating the frequency of characters.

In this program, the frequency of each character in each string is calculated and then compared with the frequency of the other string.

Code:

#include <stdio.h>
int anagram(char [], char []);
int main()
{
char i[100], j[100];
printf("two strings must be entered\n");
gets(i);
gets(j);
//checking anagrams
if (anagram(i, j) == 1)
printf("The given two strings are anagrams\n");
else
printf("The given two strings are not anagrams\n")
return 0;
}
int anagram(char i[], char j[])
{
int first1[26] = {0}, second1[26] = {0}, d=0;
// the frequency of characters in the first string is calculated
while (i[d] != '\0')
{
first1[i[d]-'a']++;
d++;
}
d = 0;
while (j[d] != '\0')
{
second1[j[d]-'a']++;
d++;
}
// the frequency of characters in the second string is calculated
for (d = 0; d < 26; d++)
{
if (first1[d] != second1[d])
return 0;
}
return 1;
}

Output:

two strings are anagrams or not

Example #4

  • C Program to find if the given two strings are anagrams or not by converting to ASCII values of alphabets.

In this program, the ASCII values of each character in one string is found out and then compared with the ASCII values of the other string.

Code:

#include<stdio.h>
#include<stdbool.h>
#include<string.h>
# define NO_OF_CHARACTERS 26
// Checking if the given strings are anagrams using functions
bool Anagram(char *Test1, char *Test2)
{
// two count arrays are created and initialized to 0
int Count1[NO_OF_CHARACTERS] = {0};
int Count2[NO_OF_CHARACTERS] = {0};
int r=0;
if (strlen(Test1) != strlen(Test2))
return false;
// count is incremented in count array for each character in the given input strings
//the ascii value of 'a' is 97
for (r = 0; Test1[r] && Test2[r]; r++)
{
Count1[Test1[r]-97]++;
Count2[Test2[r]-97]++;
}
// count arrays are compared by using the assigned value to NO_OF_CHARACTERS
for (r = 0; r < NO_OF_CHARACTERS; r++)
if (Count1[r] != Count2[r])
return false;
return true;
}
int main()
{
char Test1[] = "grab";
char Test2[] = "brag";
if (Anagram(Test1, Test2))
printf("The two strings are anagram of each other");
else
printf("The two strings are not anagram of each other");
return 0;
}

Output:

two strings

Conclusion

In this tutorial, we understand the concept of anagrams through definitions and examples. And then understand the algorithm to check if the given two input strings are anagram or not. Then we understand different C program types to check if the given strings are anagram or not along with their output’s snapshots after execution.

Recommended Articles

This is a guide to Anagram Program in C. Here we discuss the introduction, algorithm, and examples of Anagram Program in C. You may also have a look at the following articles to learn more –

  1. Anagram in C++
  2. Anagram Program in Java
  3. Anagram Program in Python
  4. Function Prototype in C

C Programming Training (3 Courses, 5 Project)

3 Online Courses

5 Hands-on Projects

34+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
C Programming Tutorial
  • C programs
    • Patterns in C Programming
    • Star Patterns in C
    • Number Patterns in C
    • Swapping in C
    • Reverse Number in C
    • Palindrome in C Program
    • Factorial in C
    • Fibonacci Series in C
    • Square Root in C
    • Random Number Generator in C
    • Prime Numbers in C
    • Escape Sequence in C
    • Reverse String in C
    • Leap Year Program in C
    • Anagram Program in C
    • Strong Number in C
    • String Concatenation in C
    • C Programming Matrix Multiplication
    • Decimal to Octal in C
    • Expression Evaluation in C
    • Decimal to Hexadecimal in C
  • Basic
    • Introduction to C
    • What is C
    • Career in C Programming
    • Advantages of C
    • How to Install C
    • Best C Compilers
    • Data Types in C
    • Variables in C
    • C Keywords
    • C Command
    • Command Line Arguments in C
    • C Literals
    • Constants in C
    • Unsigned Int in C
    • String in C
  • Pointers
    • Pointers in C
    • Null pointer in C
    • Function Pointer in C
    • Double Pointer in C
    • Void Pointer in C
    • Const Pointer in C
    • Dangling Pointers in C
    • Pointer Arithmetic in C
  • Operators
    • C Operators
    • Arithmetic Operators in C
    • Relational Operators in C
    • Assignment Operators in C
    • Logical Operators in C
    • Conditional Operator in C
    • Modulus Operator in C
    • Ternary Operator in C
    • Address Operator in C
    • Unary Operator in C
    • Operators Precedence in C
    • Left Shift Operator in C
  • Control Statement
    • Control Statements in C
    • If Statement in C
    • If-else Statement in C
    • Else if Statement in C
    • Nested if Statement in C
    • #else in C
    • Structure Padding in C
    • Nested Structure in C
    • Continue Statement in C
    • Break Statement in C
    • Switch Statement in C
    • Goto Statement in C
  • Loops
    • Loops in C
    • For Loop in C
    • While Loop in C
    • Do While Loop in C
    • Nested Loop in C
    • Infinite Loop in C 
  • Function
    • Math Functions in C
    • Hashing Function in C
    • Recursive Function in C
    • Power Function in C
    • fputs in C
    • C puts() Function
    • fprintf() in C
    • fseek() in C
    • Stderr in C
    • ASCII Value in C
    • strcat() in C
    • Inline Function in C
    • sizeof() in C
    • Function Prototype in C
    • C ftell()
  • Array
    • Arrays in C Programming
    • 2-D Arrays in C
    • 3D Arrays in C
    • Multidimensional Array in C
    • Array Functions in C
    • Strings Array in C
  • Sorting
    • Sorting in C
    • Heap Sort in C
  • Advanced
    • Constructor in C
    • Encapsulation in C
    • C Storage Classes
    • Static Keyword in C
    • File Handling in C
    • Queue in C
    • Hexadecimal in C 
    • typedef in C
    • Memory Allocation in C
    • Linked List in C
    • Volatile in C
    • Tokens in C
    • Expression in C
    • Regular Expression in C
    • Error Handling in C
    • Types of Errors in C
    • Preprocessor in C
    • Preprocessor Directives in C
    • fscanf() in C
    • #Pragma in C
    • #ifndef in C
    • #undef in C
    • Macros in C
  • Interview question
    • C Programming Interview Questions

Related Courses

C Programming Training Course

C++ Training Course

Java Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - C Programming Training (3 Courses, 5 Project) Learn More