Answer to Question #198277 in C for Prasanna

Question #198277

Design a C program to check whether the given strings are Is a Anagram or is not an Anagram".


1
Expert's answer
2021-05-26T06:25:13-0400
#include <stdio.h>


int isAnagram(char string1[], char string2[]);


//The start point of the program
int main()
{
	char string1[100];
	char string2[100];
	//get first string
	printf("Enter string 1: ");
	gets(string1);
	//get second string
	printf("Enter string 2: ");
	gets(string2);
	//Display result
	if (isAnagram(string1, string2)){
		printf("The strings are anagram\n");
	}else{
		printf("The strings are not anagram.\n");
	}
	
	getchar();
	getchar();
	return 0;
}
//This function checks if the two string are anagram
int isAnagram(char string1[], char string2[])
{
	int string1Counter[26] = {0};
	int string2Counter[26] = {0};
	int index=0;
	while(string1[index] != '\0') {
		string1Counter[string1[index]-'a']++;
		index++;
	}
	index = 0;
	while (string2[index] != '\0') {
		string2Counter[string2[index]-'a']++;
		index++;
	}


	for (index = 0; index < 26; index++){
		if (string1Counter[index] != string2Counter[index]){
			return 0;
		}
	}
	
	return 1;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS