Search code examples
canagram

"Anagram" program written in C


Full disclosure I am a college student working on a homework assignment. I am not necessarily looking for a direct answer to my question but more a nudge in the right direction. So here is my problem. I have to write a C program that takes in 2 command line arguments, one is a file containing a list of words and the other is a single word. Now the reason I have the word anagram in quotation marks is because it's not really an anagram.

Here are the problem requirements: I need to take the word from the command line (dog) and compare it to the dictionary list (doggie). If the letters in the command line word exist then I need to output a message like You can't spell "doggie" without "dog"! So I am simply checking if the letters from the command line argument exist in the words in the dictionary file.

Here is what I have so far:

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

#define MAX_WORD_LENGTH 80

int anagram(char a[], char b[]);

int main(int argc, char *argv[]) {
    if (argc != 3) {
        fprintf(stderr, "Usage: %s <list> <goal>\n", argv[0]);
        return -1;
    }
    FILE *file = fopen(argv[1], "r");

    if (file == 0) {
        fprintf(stderr, "%s: failed to open %s\n", argv[0], argv[1]);
    } else {
        char cmdLn[MAX_WORD_LENGTH], comp[MAX_WORD_LENGTH];
        strcpy(cmdLn, argv[2]);
        while (fgets(comp, sizeof comp, file) != NULL) {
            strtok(comp, "\n");
            int flag = anagram(cmdLn, comp);
            if (flag == 1) {
                printf("You can't spell \"%s\" without \"%s\".\n", cmdLn, comp);
            } else {
                printf("There's no \"%s\" in \"%s\".\n", cmdLn, comp);
            }
        }
        fclose(file);
    }
    return 0;
}

int anagram(char a[], char b[]) {

    return 1;
}

So I need to figure out an algorithm to compare each character of the command line word with each character of the word in the dictionary file. If I find each letter I from the anagram function I return 1 if I don't I return 0. I simply can't figure out how to approach this question. Any help would be greatly appreciated.

EDIT: To clarify, I can assume all letters are lower case in both the dictionary file and the command line. Also each word can be no longer than 80 characters and there will be no numbers in the words.


Solution

  • A typical, higher level language approach would be to use a set or hash of the letters. But let's keep it simple:

    Make a copy of the command line word.

    Loop through the letters in the file word: Loop through the letters in the copy word: If a letter from each matches, strike out that letter in the copy word (e.g. change it to *)

    After the loops, if all the letters of the copy word are gone (i.e. stars), it's a match

    int anagram(char *a, char *b) {
    
        size_t counter = 0, a_len = strlen(a), b_len = strlen(b);
    
        char *c = strdup(a);
    
        for (int j = 0; j < b_len; j++) {
            for (int i = 0; i < a_len; i++) {
                if (c[i] == b[j]) {
                    c[i] = '*';
                    counter++;
                    break;
                }
            }
        }
    
        free(c);
    
        return (counter == a_len);
    }
    

    You'll need to fix the above to ignore case.