Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

in C write a program that checks if two strings are anagrams. A string is an anagram of another string if the letters in the

in C write a program that checks if two strings are anagrams. A string is an anagram of another string if the letters in the first string can be rearranged to form the second string. For example army and Mary are anagrams because the letters in army can be rearranged to form Mary.

Use arrays only basic(no flags or gets)

Main function can only declare vars or call functions

Hints

Don't forget that ou can add to and subtract from characters

'B' - 'A' is 1

2 + 'A' is 'C'

test case

Example 1

Please enter the first word: MaRy Please enter the second word: arMY MaRy is an anagram of arMY

Example 2

Please enter the first word: dog Please enter the second word: god dog is an anagram of god

Example 3

Please enter the first word: bob Please enter the second word: bobs bob is NOT an anagram of bobs

Example 4

Please enter the first word: aap Please enter the second word: pap aap is NOT an anagram of pap

-max limit 20

I need help with my program it returns the alphabet letter after the one i actually want heres my code if anyone can fix it or show me a better way and do tolower instead of the function I have currently thanks.

#include  #include  void anagram() { char s1[20]={0}; char s2[20]={0}; int counter1[20]; int counter2[20]; int temp = 0; int length1 = strlen(s1); int length2 = strlen(s2); char alphabet[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',}; printf("Please enter the first word "); scanf("%s", s1); printf("Please enter the second word "); scanf("%s", s2); //to make lowercase for (int i = 0; alphabet[i] != '\0'; i++) { if (alphabet[i] >= 'A' && alphabet[i] <= 'Z') { alphabet[i] = alphabet[i] + 32; } } for (int i = 0; i < 26; i++) { for (int j = 0; j < length1; j++) if (alphabet[i] == s1[j]) { counter1[i] = 0; } counter1[i]++; } for (int i = 0; i < 26; i++) { for (int j = 0; j < length2; j++) if (alphabet[i] == s2[j]) { counter2[i] = 0; } counter2[i]++; } for (int i = 0; i < 26; i++) { if (counter1[i] == counter2[i]) temp++; } if (temp == length1 && temp == length2) { printf("%s is an anagram of %s ", s1, s2); } else { printf("%s is NOT an anagram of %s ", s1, s2); } } int main(){ anagram(); return 0; } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Database Experts Guide To Database 2

Authors: Bruce L. Larson

1st Edition

0070232679, 978-0070232679

More Books

Students also viewed these Databases questions