Albright
Albright

Reputation: 31

"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.

Upvotes: 1

Views: 1184

Answers (2)

cdlane
cdlane

Reputation: 41872

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.

Upvotes: 1

anita2R
anita2R

Reputation: 192

Read a word from file
Use an int as a flag - set it to 1 (meaning all letters in test word are present in word from file) Loop through the test word taking one letter at a time - remember a string can be accessed as an array of characters.

Use strchr to test if the letter appears in the word read from file.
If it does not appear (look at what strchr returns for not found), change the flag to 0 and break out of the comparison loop

Print a message depending on the flag value - if flag is still 1 then all letters were present, else at least one letter was not present.

    /* set flag to 'letters all present' */
    int flag = 1;
    /* test word from file for each letter in test word */
    for (n = 0; n < strlen(word);  n++) {
        if(strchr(line, word[n]) == NULL) {
            /* set flag to letter not present */
            flag = 0;
            break;
        }
    }

Upvotes: 0

Related Questions