Question

Do not use global variables. Pass your arguments by value and by reference (address). Using arrays...

Do not use global variables. Pass your arguments by value and by reference (address). Using arrays and modular programming techniques (i.e. functions), write a C-program which accepts two strings and determines whether one string is an anagram of the other, that is, whether one string is a permutation of the characters in the other string. For example, “dear” is an anagram of “read”, as is “dare”. Also, anagrams of “monday” could be “don may” or “do many”, but not “mad”. Similarily, “elvis” “lives” ;-) Your program shall output the two strings and a statement to indicate whether they are anagrams. You may assume the strings will be less than 20 characters in length and will contain only spaces and alphabetic characters. Hint: use tolower( ).

Check List:

1. Programmer’s block (title, programmer’s name, description of program)

2. Style: (indentation, inline comments, meaningful variable names)

3. Use of modular techniques. There are at least one pass by reference and one pass by value functions.

4. Make sure you have back-ups.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

HI, Please find my implementation.

Please let me know in case of any issue.

My implementation 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 <stdio.h>
#define NO_OF_CHARS 256

/* function to check whether two strings are anagram of
each other */
int checkAnagram(char *str1, char *str2)
{
// Create a count array and initialize all values as 0
int count[NO_OF_CHARS] = {0};
int i;

// For each character in input strings, increment count in
// the corresponding count array
for (i = 0; str1[i] && str2[i]; i++)
{
count[str1[i]]++;
count[str2[i]]--;
}

// If both strings are of different length. Removing this condition
// will make the program fail for strings like "aaca" and "aca"
if (str1[i] || str2[i])
return 0;

// See if there is any non-zero value in count array
for (i = 0; i < NO_OF_CHARS; i++)
if (count[i])
return 0;
return 1;
}

int main()
{
char str1[50];
char str2[50];
printf("Enter first string: ");
scanf("%s", str1);
printf("Enter second string: ");
scanf("%s", str2);
  

if(checkAnagram(str1, str2) )
printf("The two strings are anagram of each other\n");
else
printf("The two strings are not anagram of each other\n");
return 0;
}

Add a comment
Know the answer?
Add Answer to:
Do not use global variables. Pass your arguments by value and by reference (address). Using arrays...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using loops with the String and Character classes. You can also use the StringBuilder class to...

    Using loops with the String and Character classes. You can also use the StringBuilder class to concatenate all the error messages. Floating point literals can be expressed as digits with one decimal point or using scientific notation. 1 The only valid characters are digits (0 through 9) At most one decimal point. At most one occurrence of the letter 'E' At most two positive or negative signs. examples of valid expressions: 3.14159 -2.54 2.453E3 I 66.3E-5 Write a class definition...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT