Question

Write a program in C to check whether two given strings are an anagram. From the...

Write a program in C to check whether two given strings are an anagram. From the following prototype, statement create a function that determines whether two strings are anagrams.

Test Data:

Input the first String: spare

Input the second String: pears

Expected output:

spare and pears are an anagram

updated

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

C code

============================================================================================

#include <stdio.h>

int checkAnagram(char [], char []);
int main()
{
   char s1[100],s2[100];
   int k;
   printf("Input the first String: ");
   scanf("%s",s1);
   printf("Input the second String: ");
   scanf("%s",s2);
   k=checkAnagram(s1,s2);
   if(k==1)
   printf("%s and %s are an anagram\n",s1,s2);
   else
   printf("%s and %s are not anagram\n",s1,s2);
  
   return 0;
}

int checkAnagram(char a[], char b[])
{
   int first[26] = {0}, second[26] = {0}, i = 0;

// Calculating frequency of characters of first string
while (a[i] != '\0')
{
first[a[i] - 'a']++;
i++;
}
i = 0;
// Calculating frequency of characters of second string
while (b[i] != '\0')
{
second[b[i] -'a']++;
i++;
}
  
// Comparing frequency of characters
for (i = 0; i < 26; i++)
{
if (first[i] != second[i])
return 0;
}
return 1;
}

============================================================================================

Output

Add a comment
Know the answer?
Add Answer to:
Write a program in C to check whether two given strings are an anagram. From the...
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
  • Anagram Detector Due by Friday 12 April 2019 11:59 PM Program Overview: This program will be able...

    I need a program in fortran 95 that can detect if two strings are anagrams Anagram Detector Due by Friday 12 April 2019 11:59 PM Program Overview: This program will be able to determine if two inputted texts are anagrams of one another Relevant Details and Formulas: An anagram of a text is a rearrangement of the letters such that it forms another, usually intelligible, set of words. Capitalization is not important and any white space, punctuation, or other non-letter...

  • Anagram Difference We define an anagram to be a word whose characters can be rearranged to...

    Anagram Difference We define an anagram to be a word whose characters can be rearranged to create another word. Given two strings, we want to know the minimum number of characters in either string that we must modify to make the two strings anagrams. If it's not possible to make the two strings anagrams, we consider this number to be -t For example: tea and ate are anagrams, so we would need to modity 0 characters tea and toe are...

  • Anagram Detector Due by Friday 12 April 2019 11:59 PM Program Overview: This program will be...

    Anagram Detector Due by Friday 12 April 2019 11:59 PM Program Overview: This program will be able to determine if two inputted texts are anagrams of one another. Relevant Details and Formulas: An anagram of a text is a rearrangement of the letters such that it forms another, usually intelligible, set of words. Capitalization is not important and any white space, punctuation, or other non-letter symbols should be ignored. Program Specification: * Prompt the user for a pair of text...

  • Write a program thet uses the function strcmp() to compare two strings input by the user....

    Write a program thet uses the function strcmp() to compare two strings input by the user. The program should state whether the first string is less then, equal to, or greater then the second string 7. Write a program thet uses the function strcmp() to compare two strings input by the user. The program should state whether the first string is less then, equal to, or greater then the second string

  • ****Using c++ (Check Substrings) Write the following function to check whether string s1 is a substring...

    ****Using c++ (Check Substrings) Write the following function to check whether string s1 is a substring of string s2. The function returns the first index in s2 if there is a match. Otherwise, return -1. int indexOf(const string& s1, const string& s2) Write a test program that reads two strings and checks whether the first string is a substring of the second string.

  • must be done in C Write a program that uses function strncmp to compare two strings...

    must be done in C Write a program that uses function strncmp to compare two strings input by the user. The program should input the number of characters to be compared, then display whether the first string is less than, equal to, or greater than the second string.

  • 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”....

  • Python code Write a program that will ask the user to input two strings, assign the...

    Python code Write a program that will ask the user to input two strings, assign the strings to variables m and n If the first or second string is less than 10 characters, the program must output a warning message that the string is too short The program must join the strings m and n with the join function The output must be displayed. Please name the program and provide at least one code comment (10)

  • Write a program that uses String method regionMatches to compare two strings input by the user....

    Write a program that uses String method regionMatches to compare two strings input by the user. The program should prompt the user to enter two strings, the starting index in the first string, the starting index in the second string, and the number of characters to be compared. The program should print whether or not the strings are equal, (Ignore the case of the characters during comparison.) 四SAMPLE RUN #1: java StringCompare Highlight: None D Show Highlighted Only Interactive Session...

  • Question / of 2. Case insensitive string compare: [40 marks/ Write a function that compares two...

    Question / of 2. Case insensitive string compare: [40 marks/ Write a function that compares two strings while ignoring the case of alphabetical characters. Your program will take two strings as input from the user and use your function to compare them. Assume a maximum C-string size of 1000 characters. Make sure your code works for any input number, not just the test cases. Your code will be tested on other test cases not listed here. Please properly comment your...

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