Complete the C function described below. Some marks will be given for the qualit
ID: 3575669 • Letter: C
Question
Complete the C function described below. Some marks will be given for the quality of your code. List all assumptions but please note that unreasonable assumptions will be penalized. The input parameters must not be modified by your solution./* * is_anagram * * input: * -- char *str1, *str2: two existing strings (character arrays) * * purpose: to return 1 if str1 is an anagram of str2; return 0 otherwise * * examples of anagrams: * -- An anagram for "Tom Cruise" is "So I'm Cuter!" * -- An anagram for "Waitress" is "A stew, Sir?" * -- An anagram for "software process" is "Cafe's worst prose" * * Note that letter case, whitespace and punctuation are ignored. * You can use the "isalpha()" and "tolower()" functions of C to help you with * your work. The value of (int) 'a' is 97. */int is_anagram(char *str1, char *str2)Explanation / Answer
// C code to check anagram
# include <stdio.h>
#include <ctype.h>
# define TOTALCHARACTERS 256
void remove(char *str)
{
int i,j;
for(i = 0; str[i] != ''; ++i)
{
while (!( (str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z') || str[i] == '') )
{
for(j = i; str[j] != ''; ++j)
{
str[j] = str[j+1];
}
str[j] = '';
}
}
}
int is_anagram(char *str1, char *str2)
{
remove(str1);
remove(str2);
int i;
int c1[TOTALCHARACTERS] = {0};
int c2[TOTALCHARACTERS] = {0};
for (i = 0; str1[i] && str2[i]; i++)
{
if(isalpha(str1[i]) == false)
c1[tolower(str1[i])]++;
if(isalpha(str2[i]) == false)
c2[tolower(str2[i])]++;
}
for (i = 0; i < TOTALCHARACTERS; i++)
if (c1[i] != c2[i])
{
printf("%c",i);
return 0;
}
// if all test passed, return true
return 1;
}
int main()
{
char str1[TOTALCHARACTERS];
char str2[TOTALCHARACTERS];
printf("Enter str1: ");
scanf("%[^ ]%*c",str1);
printf("Enter str2: ");
scanf("%[^ ]%*c",str2);
if ( is_anagram(str1, str2) == 1)
printf(" Strings are Anagram ");
else
printf(" Strings are not Anagram ");
return 0;
}
/*
output:
Enter str1: software process
Enter str2: Cafe's worst prose
Strings are Anagram
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.