Question

in c,

5. Write a function that tests whether two words are anagrams. The function returns 1 if the two words are anagrams, returns 0 otherwise. Two words are anagrams if they are permutations of the same letters. For example, smartest and mattress are anagrams and dumbest and stumble are not anagrams. Assume wordl and word2 are null-terminated strings containing arbitrary lower-case alphabetic letters. Hint: use an array of 26 integers to keep track of how many times each letter is in a word. int are anagrams (char *wordl, char word2)

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

int are_anagrams( char *word1, char *word2 )

{

    // if the two words are not of the same length

    if( strlen(word1) != strlen(word2) )

        return 0;

   

    // arr1 stores the count of each character in word1

    // arr2 stores the count of each character in word2

    int arr1[26], arr2[26];

   

    int i;

   

    // initialize the elements to 0

    for( i = 0 ; i < 26 ; i++ )

    {

        arr1[i] = 0;

        arr2[i] = 0;

    }

   

    for( i = 0 ; i < strlen(word1) ; i++ )

    {

        // index of a = 0

        // .

        // .

        // .

        // index of z = 25

        int index = (int)word1[i] - 97;

       

        arr1[index]++;

    }

   

    for( i = 0 ; i < strlen(word2) ; i++ )

    {

        // index of a = 0

        // .

        // .

        // .

        // index of z = 25

        int index = (int)word2[i] - 97;

       

        arr2[index]++;

    }

   

    for( i = 0 ; i < 26 ; i++ )

    {

        // if the count of current character are not equal

        if( arr1[i] != arr2[i] )

            return 0;

    }

   

    // if the two words are anagrams

    return 1;

}

Add a comment
Know the answer?
Add Answer to:
in c, 5. Write a function that tests whether two words are anagrams. The function returns...
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
  • C++ PROGRAMMING Implement a bool function whose header is given below. Inside the function, declare, ask,...

    C++ PROGRAMMING Implement a bool function whose header is given below. Inside the function, declare, ask, and get a character from the user. The function will return true if the character the user enters is NOT in either of the words, otherwise it will return false. Keep in mind that uppercase and lowercase letters are not the same. You will need to change both words and the character input by the user to the same case (upper or lower) -...

  • # DISCUSSION SECTION WORK: # # 1. STUDENTS: download this file, ds4.py, and wordsMany.txt, from #...

    # DISCUSSION SECTION WORK: # # 1. STUDENTS: download this file, ds4.py, and wordsMany.txt, from # http://www.cs.uiowa.edu/~cremer/courses/cs1210/etc/ds4/ # Save both in the same folder. # # 2. TA (aloud) and STUDENTS: Read the comments from START HERE! (just after these instructions) # to definition of anagramInfo function. Discuss any questions about what the functions should do. # # 3. TA demonstrate running anagramInfo("wordsMany.txt") on this unchanged file, to # see that it behaves reasonably despite having incomplete anagram-testing functions. #...

  • Create a C project named login_l06t1. Write and test a function strnclean that takes two strings...

    Create a C project named login_l06t1. Write and test a function strnclean that takes two strings as parameters, target and source. Using the ctype library, copy only the alphabetic characters from source to target, and make the characters lower case. Ex: source: David Brown! target: davidbrown Prototype: void strnclean(char *target, const char *source); How do i do this using the C type library below! int isalnum(int c) Returns non-zero (true) if c is an alphanumeric character, zero otherwise. int isalpha(int...

  • Write a function named words_in_both that takes two strings as parameters and returns a set of...

    Write a function named words_in_both that takes two strings as parameters and returns a set of the words contained in both strings. You can assume all characters are letters or spaces. Capitalization shouldn't matter: "to", "To", "tO", and "TO" should all count as the same word. The words in the set should be all lower-case. For example, if one string contains "To", and the other string contains "TO", then the set should contain "to". The file must be named: words_in_both.py...

  • write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy....

    write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy. The function should be passed 2 parameters, as illustrated in the prototype below. int pow_xy(int *xptr, int y); Assuming that xptr contains the address of variable x, pow_xy should compute x to the y power, and store the result as the new value of x. The function should also return the result. Do not use the built-in C function pow. For the remaining problems,...

  • Write a function named words_in_both that takes two strings as parameters and returns a set of...

    Write a function named words_in_both that takes two strings as parameters and returns a set of the words contained in both strings. You can assume all characters are letters or spaces. Capitalization shouldn't matter: "to", "To", "tO", and "TO" should all count as the same word. The words in the set should be all lower-case. For example, if one string contains "To", and the other string contains "TO", then the set should contain "to". You can use Python's split() funciton,...

  • using basic c++ and use no functions from c string library b) Write a fragment of...

    using basic c++ and use no functions from c string library b) Write a fragment of code (with declarations) that reads a pair of words from the keyboard and determines how many letters in corresponding positions of each word are the same. The words consist only of the letters of the English alphabet (uppercase and lowercase). For example, if the words are coat and CATTLE, the following output should be generated: 012245 0122 matching letter in position 0 t is...

  • Write a function called most_consonants(words) that takes a list of strings called words and returns the...

    Write a function called most_consonants(words) that takes a list of strings called words and returns the string in the list with the most consonants (i.e., the most letters that are not vowels). You may assume that the strings only contain lowercase letters. For example: >>> most_consonants(['python', 'is', 'such', 'fun']) result: 'python' >>> most_consonants(['oooooooh', 'i', 'see', 'now']) result: 'now' The function that you write must use a helper function (either the num_vowels function from lecture, or a similar function that you...

  • Please Use C++ Language. Thank you. Please I need the actual code. Donot post psudocode!! ​And...

    Please Use C++ Language. Thank you. Please I need the actual code. Donot post psudocode!! ​And also I have codes but just donot work so make sure that it works. Requested files: CrosswordGenerator.cpp, CrosswordGenerator.h, CrosswordGenerator_test.cpp CrosswordGenerator - Write a program that helps to generate a crossword puzzle by organizing words that share letters.   For this assignment, you will write a program that forms the basis of a crossword puzzle generator. In order to create a crossword puzzle you need to...

  • C++ Language: TestCase: Write a function, called "sort Lbefore e that can sort words into whether or not they conf...

    C++ Language: TestCase: Write a function, called "sort Lbefore e that can sort words into whether or not they conform to the 1 beforee, except after c rule, See htrps//en.oxtorddictionaries.com/spelling/-before-e-except-after-cl. This function takes 4 references to vectors of strings as parameters . The first vector is the input consisting of lowercase words (only alphabetic charactersl. The next vector should be filled with words that follow the rule The next vector should be filled with words that don't have ie or...

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