An anagram is a type of word play, the result of rearranging the letters of a wo
ID: 3534927 • Letter: A
Question
An anagram is a type of word play, the result of rearranging the letters of a word or phrase to produce a new word or phrase, using all the original letters exactly once; for example orchestra can be rearranged into carthorse. Write a program that scans two strings and checks if the two strings are anagram to each other. You need to provide a way to stop the program properly. Using Ctrl+D is not required.
Your program should call anagram ( ) function to check if the two strings are anagram.
int anagram(char *s1, char *s2);
This function should return 1 if s1 and s2 are anagrams, otherwise returns 0.
You can use bubble sort to sort the strings.
You can use strcmp( ) to check two sorted strings.
Assume that strings only consist of letters, no space, no digit, etc.
Assume each string consists of less than 15 letters.
Example:
Explanation / Answer
This method assumes that the set of possible characters in both strings is small. In the following implementation, it is assumed that the characters are stored using 8 bit and there can be 256 possible characters. 1) Create count arrays of size 256 for both strings. Initialize all values in count arrays as 0. 2) Iterate through every character of both strings and increment the count of character in the corresponding count arrays. 3) Compare count arrays. If both count arrays are same, then return true. # include # define NO_OF_CHARS 256Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.