Eternal Eternal - 1 month ago 8
C Question

Read from a text file and use each line to compare if they are anagrams

I must modify my program to accept input from
a file called anagrams.txt.This file should have two strings per line, separated by the # character. My program should read
each pair of strings and report back if each pair of strings is an anagram. For example consider the following content of anagrams.txt:

hello#elloh

man#nam

Astro#Oastrrasd


Your program should print out the following:

hello#elloh - Anagrams!

man#nam - Anagrams!

Astro#Oastrrasd- Not anagrams!

I should compile in g++

Here is the code to read from text:

int main()
{
char input[30];




if(access( "anagrams.txt", F_OK ) != -1) {

FILE *ptr_file;
char buf[1000];

ptr_file =fopen("anagrams.txt","r"); if (!ptr_file)
return 1;

while (fgets(buf,1000, ptr_file)!=NULL)
printf("%s",buf);

fclose(ptr_file);
printf("\n");
}

else{ //if file does not exist
printf("\nFile not found!\n");
}


return 0;
}


Code to find if the text are anagrams:

#include <stdio.h>

int find_anagram(char [], char []);

int main()
{
char array1[100], array2[100];
int flag;

printf("Enter the string\n");
gets(array1);
printf("Enter another string\n");
gets(array2);
flag = find_anagram(array1, array2);
if (flag == 1)
printf(" %s and %s are anagrams.\n", array1, array2);
else
printf("%s and %s are not anagrams.\n", array1, array2);
return 0;
}

int find_anagram(char array1[], char array2[])
{
int num1[26] = {0}, num2[26] = {0}, i = 0;

while (array1[i] != '\0')
{
num1[array1[i] - 'a']++;
i++;
}
i = 0;
while (array2[i] != '\0')
{
num2[array2[i] -'a']++;
i++;
}
for (i = 0; i < 26; i++)
{
if (num1[i] != num2[i])
return 0;
}
return 1;
}

Answer

You can try something like this:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define MAXLINE 1000
#define MAXLETTER 256

int is_anagram(char *word1, char *word2);
void check_lines(FILE *filename);
int cmpfunc(const void *a, const void *b);
void convert_to_lowercase(char *word);

int
main(int argc, char const *argv[]) {
    FILE *filename;

    if ((filename = fopen("anagram.txt", "r")) == NULL) {
        fprintf(stderr, "Error opening file\n");
        exit(EXIT_FAILURE);
    }

    check_lines(filename);

    fclose(filename);

    return 0;
}

void
check_lines(FILE *filename) {
    char line[MAXLINE];
    char *word1, *word2, *copy1, *copy2;

    while (fgets(line, MAXLINE, filename) != NULL) {
        word1 = strtok(line, "#");
        word2 = strtok(NULL, "\n");

        copy1 = strdup(word1);
        copy2 = strdup(word2);

        convert_to_lowercase(copy1);
        convert_to_lowercase(copy2);

        if (is_anagram(copy1, copy2)) {
            printf("%s#%s - Anagrams!\n", word1, word2);
        } else {
            printf("%s#%s - Not Anagrams!\n", word1, word2);
        }
    }
}

void
convert_to_lowercase(char *word) {
    int i;

    for (i = 0; word[i] != '\0'; i++) {
        word[i] = tolower(word[i]);
    }
}

int
is_anagram(char *word1, char *word2) {

    qsort(word1, strlen(word1), sizeof(*word1), cmpfunc);
    qsort(word2, strlen(word2), sizeof(*word2), cmpfunc);

    if (strcmp(word1, word2) == 0) {
        return 1;
    } 
    return 0;
}

int
cmpfunc(const void *a, const void *b) {
    if ((*(char*)a) < (*(char*)b)) {
        return -1;
    }

    if ((*(char*)a) > (*(char*)b)) {
        return +1;
    }

    return 0;
} 
Comments