Anagram In C

Algorithm of the Anagram string

Following are the algorithms of the anagram string in C, as follows:

  • Input two strings from the user.
  • Check the length of each string. If the length of the first string is not equal to the second string, the strings are not an anagram.
  • If the length of both strings is equal, convert the string's characters into lower case letters that make the comparison of the string easier.
  • After this, sort the characters of the strings using the built-in function of the C library to sort the strings. And if there is no built-in function, we convert the string to a character array.
  • We must sort the converted string into a character array.
  • And last, we check for the equality of the strings.

Example 1: Script to verify if a given string is an anagram by utilizing a custom function

Example

#include <stdio.h>

// function definition
int get_anagrm (char [],char []);

int main ()
{
	// declaration of the array
	char arr1 [50], arr2 [50];
	int count;
	
	printf (" Enter the first string: \n ");
	scanf (" %s", arr1);
	
	
	printf (" Enter the second string: \n ");
	scanf (" %s", arr2);
	
	// call function
	count = get_anagrm (arr1, arr2);
	
	// use if-else statement to validate both strings are anagram or not.
	
	if (count == 1)
	{
		printf (" %s and %s strings are an anagram of each other. \n", arr1, arr2);
	}
	else
	{
		printf (" %s and %s strings are not an anagram of each other. \n", arr1, arr2);
	}
	
	return 0;
}

// function defnition
int get_anagrm (char arr1[], char arr2[])
{
	// create two num arrays and initialize their value as 0
	int num1[20] = {0}, num2[20] = {0}, i = 0;
	
	// use while loop to check arr1 is not null
	while (arr1[i] != '\0')
	{
		num1[arr1[i] -'a']++;
		i++;
	}
	
	i = 0;
	
	// use while loop to check arr2 is not null
	while (arr2[i] != '\0')
	{
		num2[arr2[i] -'a']++;
		i++;
	}
	
	for ( i = 0;i <20; i++)
	{
		if ( num1[i] != num2[i])
			return 0;
	}
	return 1;
}

Output

Output

Enter the first string:
pine
Enter the second string
nipe
pine and nipe strings are an anagram of each other.

2nd time run:
Enter the first string:
pine
Enter the second string
nip
pine and nip strings are not an anagram of each other.

A sample code to verify if a string is an anagram by employing a nested for loop.

Example

#include <stdio.h>
#include <stdbool.h>
#include <string.h>

// define the no. of characters
#define No_Character 30

// use bool function to check the strings are anagrams
bool strAna (char *st1, char *st2)
{
	// create two arrays and initialized with 0
	int arr1[No_Character] = {0};
	int arr2[No_Character] = {0};
	
	int n = 0;
	// check the length of each string
	if (strlen (st1) != strlen (st2))
		return false;
		
	// use for loopto incremnet the array index
	for ( n = 0; st1[n] && st2[n]; n++)
	{
		// increment the string character
		// ascii value of a is 97
		arr1[st1[n]-97]++;
		arr2[st2[n]-97]++;
		}	
		
	// 	arr1 is compared using the assigned value
	for (n = 0; n < No_Character; n++)
	{
		if (arr1[n] != arr2[n])
			return false;
		return true;	
	}
}

int main ()
{
	char st1[50], st2[50];
	
	printf (" Input the first string: ");
	gets(st1);
	
	printf (" Input the second string: ");
	gets(st2);
	// call strAna function to check the anagram of the string
	if (strAna (st1, st2))
	{
		printf (" \n The first string is an anagram of the second string. ");
	}
	else
	{
		printf (" \n The first string is not an anagram of the second string. ");
	}
	return 0;
}

Output

Output

Input the first string: triangle
Input the second string: agtrinle
The first string is an anagram of the second string.

Example 3: Script to verify if two strings are anagrams using for loop and conditional statements.

Example

#include <stdio.h>
#include <string.h>
int main ()
{
	// declare the character type array
	char arr1[30], arr2[30];
	int len1, len2, num, i, j, found = 0, not_found = 0;
	
	printf (" Input the first string: ");
	scanf ("%s", arr1);
	
	printf (" Input the second string: ");
	scanf ("%s", arr2);
	
	// get the length of the strings
	len1 = strlen (arr1);
	len2 = strlen (arr2);
	
	// use if statement to check the length is equal or not
	if (len1 == len2)
	{
		num = len1; // assign the length to num variable
		for ( i = 0; i < num; i++)
		{
			found = 0;
			for ( j = 0; j < num; j++)
			{
				// check string is equal or not
				if (arr1[i] == arr2[j])
				{
					found = 1;
					break;
				}
			}
			
			if (found == 0)
			{
				not_found = 1; // assign 1 to not_found
				break;
			}
		}
		if (not_found == 1)
			printf (" \n The first string is not an anagram of the second string. ");
		else
			printf (" \n The first string is an anagram of the second string. ");	
	}
	else
		printf (" \n Both string must contains same number of character to be the anagram of string. ");
		
	return 0;	
}

Output

Output

Input the first string: SILENCE
Input the second string: LENSICE
The first string is an anagram of the second string.

Example 4: Algorithm to arrange the characters within a string in a specific order and verify if two strings are anagrams of each other

Example

#include <stdio.h>
#include <conio.h>
#include <string.h>

// declaration of the functions
void cnvt_to_lwr (char []);
void sortArr (char []);

int main ()
{
	char str1[] = "course", str2[] = "source";
	int x, y = 0;
	
	// check the length of each string
	if (strlen (str1) != strlen (str2))
	{
		printf(" The given strings are not anagram of each other. ");
		return 0;
	}
	
	else
	{
		// convert string into lowercase
		cnvt_to_lwr (str1);
		cnvt_to_lwr (str2);
		
		// call sortArr() function to sort the arrays
		sortArr (str1);
		sortArr (str2);
		for (x = 0; x < strlen (str1); x++)
		{
			// check string is equal or not
			if (str1[x] != str2[x])
			{
				printf (" The given strings are not an anagram of each other.");
				return 0;
			}
		}
		printf ( " Both strings are an anagram of each other. ");
	}
		return 0;
		
	}

// function definition here
void cnvt_to_lwr (char a1[])
{
	int num;
	for ( num = 0; num < strlen(a1) -1; num++)
	{
		a1[num] = a1[num] + 32;
	}
}

// sorting here
void sortArr (char a1[])
{
	int temp = 0, i, l;
	
	for (i = 0; i <strlen(a1) - 1; i++)
	{
		for ( l = i+1; l < strlen (a1); l++)
		{
			if (a1[i] > a1[l])
			{
				temp = a1[i];
				a1[i] = a1[l];
				a1[l] = temp;
				 
			}
		}
	}	
	
}

Output

Output

Both strings are an anagram of each other.

Input Required

This code uses input(). Please provide values below: