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

Write a C coding language program that prompts the user to enter the name of a f

ID: 3598188 • Letter: W

Question

Write a C coding language program that prompts the user to enter the name of a file. The program finds the anagrams in the file.

Enter the file name: words.txt

Output: anagrams are written to file: words.txt.ang

The program reads the content of the file and stores the words in an array of strings, the program then finds the anagrams and writes the anagrams to the output file.

1. Name your program fileIO.c. The output file name should be the same name as the input file but with an added extension of .ang. In this example, the original file name is words.txt. The output file name is then words.txt.ang. Assume the file name is no more than 100 characters. Assume the length of each line in the input file is no more than 100 characters. Assume the input file contains no more than 1000 words.

2. The following function is provided in anagram.c: int are_anagram(char *word1, char *word2); word1 and word2 are strings containing the words to be checked for anagram. The function returns 1 if the two words are anagram of each other and return 0 otherwise.

3. The output file should be in the following format. The output file contains all the anagrams.

1 inch chin

2 roast beef eat for BSE

int are_anagram(char *word1, char *word2)

{

int letter_counts[26]={0};

char *p; char *q;

int i, count =0;

char ch;

forr(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;

}

Explanation / Answer

Following are the c program for your requirement : ------------------------------->>>>>>>>>>>>>>>>>

#include<stdio.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;
}
int length(char *file){
int i = 0;
for(i;;i++){
  if(file[i] == ''){
   return i;
  }
}
}

int main(){
FILE *fp1,*fp2;
char filename[100];
char words[1000][20];
int wordcount=0;
char line[101];
int state = 0,s = 0;
printf("-------------Welcome To find anagrams----------------");
printf(" Enter The File name ....   ");
scanf("%s",&filename);
int l = length(&filename);
fp1 = fopen(filename,'r');
while(!feof(fp1)){
  fgets(line,100,fp1);
  int i = 0;
  for(i;;i++){
   if(line[i] == '')
   {
    break;
   }
   if(line[i] == ' ')
   {
    if(state == 3){
     wordcount = wordcount+1;
    }
    state = 0;
   }
   if(line[i] != ' ' && state == 3){
    s = s+1;
    words[wordcount][s] = line[i];
   }
   if(state == 0 && line[i] != ' '){
    s = 0;
    words[wordcount][s] = line[i];
    state = 3;
   }
  }
}
filename[l] = '.';
filename[l+1] = 'a';
filename[l+2] = 'n';
filename[l+3] = 'g';

fp2 = fopen(filename,'w');
int i = 0;
int j = 0;
int check = 0;
state = 0;
for(i;i<wordcount;i++){
  for(j;j<wordcount;j++){
   check = are_anagram(&words[i][0],&words[j][0]);
   if(check){
    if(state = 0){
    fprintf(fp2,"1. %s %s",words[i][0],words[j][0]);
    state = 2;
    }
    else
    fprintf(fp2," %s",words[j][0]);
   }
  }
  state = 0;
}

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote