Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

******Are_anagram function: int are_anagram(char *word1, char *word2) { int lett

ID: 3597216 • Letter: #

Question

******Are_anagram function:
int are_anagram(char *word1, char *word2)
{
int letter_counts[26]={0};
char *p; char *q;
int i, count =0;
char ch;
for(p = word1; *p!=''; p++)
if(isalpha(*p))
{
ch = tolower(*p);
letter_counts[ch - 'a']++;
}
for(q = word2; *q!=''; q++)
if(isalpha(*q))
{     ch = tolower(*q);
letter_counts[ch - 'a']--;
}
for(i =0;i<26;i++)
{
        if(letter_counts[i]==0)
count++;
}
if(count == 26)
return 1;
else
return 0;
return 0;
}
*****Content of words.txt file:
rail safety
inch
roast beef
eat for BSE
William Shakespeare
Madam Curie
lake
morning
Radium came
I am a weakish speller
Heir
hire
computer science
chin
knee
array length
leak
kiss

****this is the read_line function if you need***
int read_line(char *str, int n)
{
int ch, i =0;
while ((ch = getchar()) != ' ')
{
   if (i<n)
*str++ = ch;
}
*str= '';
return i;
}

Write the main function to find the anagram (I/O) based on are_anagram and file words.txt.
The user will enter the name of the file, for example:
Enter the text file name: Words.txt.
If they enter a name which is incorrect, then output error.
The program will restrict contents from words.txt file then find the anagram. Based on the given words.txt above, the output should be: (print out)
1.Inch
Chin
2. Roast beef
   Eat for BSE
…so on…
And also output these anagrams into a file named: words.txt.ang (same name as the input file with an extension .ang). Then out a messages:
Anagrams are written to file: words.txt.ang
Assume the filename is no more than 100 characters. Length of each line in the input file no more than 100 char. The input file contains no more than 1000 words. Use array 2-dimensional. Loops. Newbies level . DO NOT USE MALLOC , use pointers because I have not studied it yet! As simple as possible and please leave some comments. Love you!

Explanation / Answer

anagram.c

#include <stdio.h>
#include <malloc.h>

int are_anagram(char* word1, char* word2)
{
int letter_counts[26] = { 0 };
char* p;
char* q;
int i, count = 0;
char ch;
for (p = word1; *p != ''; p++)
if (isalpha(*p)) {
ch = tolower(*p);
letter_counts[ch - 'a']++;
}
for (q = word2; *q != ''; q++)
if (isalpha(*q)) {
ch = tolower(*q);
letter_counts[ch - 'a']--;
}
for (i = 0; i < 26; i++) {
if (letter_counts[i] == 0)
count++;
}
if (count == 26)
return 1;
else
return 0;
return 0;
}

char* readline(FILE** fp)
{
char* line = malloc(sizeof(char) * 100);
char ch;
int i = 0;
while (1) {
ch = fgetc(*fp);
if (ch == ' ' || ch == EOF) {
break;
}
line[i++] = ch;
}
line[i] = 0;
return i == 0 ? NULL : line;
}

int main()
{
char *lines[1000], *line, filename[100];
int found[1000];
int count = 0, i, j;
printf("Enter the text file name: ");
scanf("%s", filename);
FILE* fp = fopen(filename, "r");
if (!fp) {
printf("can not read from %s ", filename);
return 0;
}
sprintf(filename, "%s.ang", filename);
FILE* out = fopen(filename, "w");
while (1) {
line = readline(&fp);
if (line == NULL) {
break;
}
lines[count] = line;
found[count] = 0;
++count;
}
for (i = 0; i < count; ++i) {
if (!found[i]) {
for (j = 0; j < count; ++j) {
if (i != j) {
if (are_anagram(lines[i], lines[j])) {
printf("%s %s ", lines[i], lines[j]);
fprintf(out, "%s %s ", lines[i], lines[j]);
found[i] = 1;
found[j] = 1;
}
}
}
}
}
fclose(out);
fclose(fp);
return 0;
}