Anagram in C

Here is the program for a re-arranged word in c.

Two strings are said to be re-arranged words if by modifying the characters of anyone string will make the two strings equivalent.

Model:

“adssfa” and “dsasfa” are re-arranged words

“adsfa” and “sdfac” are not re-arranged words

How to Check two Strings are Anagrams or not?

So what we will do is discover the recurrence of every character in first and second string and store it in two exhibits. Presently we will check the recurrence of each character in two strings by contrasting the two clusters.

On the off chance that each character has the same recurrence, at that point, the strings are re-arranged words generally not. Underneath I have composed a C program to actualize this rationale.

On the off chance that you are finding any trouble, at that point remark underneath, I will attempt to take care of your concern.

Re-arranged word Program in C

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
 
//function to count the frequency of each character
void count_frequency(char str[],int s[])
{
	int i=0,j,count;
	while(str[i]!='\0')
	{
		j=0;
		count=0;
		while(str[j]!='\0')
		{
			if(str[i]==str[j])
				count++;
			j++;
		}
		
		s[str[i]-97]=count;
		i++;
	}
}
 
int main()
{
	char str1[100],str2[100];
	int i,j,flag=1,s1[26]={0},s2[26]={0};
	
	printf("Enter first string:");
	scanf("%s",str1);
	printf("Enter second string:");
	scanf("%s",str2);
	
	if(strlen(str1)!=strlen(str2))	//if the lengths of two strings are not equal
	{
		printf("\nStrings are not anagrams");
		exit(0);
	}
	
	count_frequency(str1,s1);
	count_frequency(str2,s2);
	
	for(i=0;i<26;++i)	//checking freuency of each character
	{
		if(s1[i]!=s2[i])
		{
			flag=0;
			break;
		}
	}
	
	if(flag)
		printf("\nStrings are anagrams");
	else
		printf("\nStrings are not anagrams");
		
	return 0;
}

Output

Anagram in C

Leave a Comment

error: Alert: Content is protected!!