Word Test

PHOTO EMBED

Tue Apr 23 2024 09:22:34 GMT+0000 (Coordinated Universal Time)

Saved by @meanaspotato #c

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

int main() {
    char word1[100], word2[100];
    int freq1[26] = {0}, freq2[26] = {0};

    // Prompt user to enter the first word
    printf("Word 1: \n");
    scanf("%s", word1);

    // Prompt user to enter the second word
    printf("Word 2: \n\n");
    scanf("%s", word2);

    // Count frequency of each letter in word1
    for (int i = 0; i < strlen(word1); i++) {
        if ((word1[i] >= 'a' && word1[i] <= 'z') || (word1[i] >= 'A' && word1[i] <= 'Z')) {
            if (word1[i] >= 'A' && word1[i] <= 'Z') {
                freq1[word1[i] - 'A']++;
            } else {
                freq1[word1[i] - 'a']++;
            }
        }
    }

    // Count frequency of each letter in word2
    for (int i = 0; i < strlen(word2); i++) {
        if ((word2[i] >= 'a' && word2[i] <= 'z') || (word2[i] >= 'A' && word2[i] <= 'Z')) {
            if (word2[i] >= 'A' && word2[i] <= 'Z') {
                freq2[word2[i] - 'A']++;
            } else {
                freq2[word2[i] - 'a']++;
            }
        }
    }

    // Compare the frequency arrays
    int match = 1;
    for (int i = 0; i < 26; i++) {
        if (freq1[i] != freq2[i]) {
            match = 0;
            break;
        }
    }

    // Print the result
    if (match) {
        printf("YES! %s and %s\n", word1, word2);
    } else {
        printf("NO!\n");
    }

    return 0;
}

content_copyCOPY

Word Test Description: The goal of the "Word Test" exercise is for the user to input two distinct words. The program must determine if the two words have exactly the same number of each alphabet letter. If they do, it should display a positive affirmation; otherwise, a negative one. This essentially tests if the two words are anagrams, although the term isn't explicitly used. Instructions: The program should start by prompting the user to enter the first word with the message: "Word 1: ". The program should then ask the user for the second word with the message: "Word 2: ". Using arrays or data structures, count the frequency of each letter in the two words. Compare the frequencies of each letter in both words. Based on the comparison, if both words contain the same number of each letter, print: "YES! [word1] and [word2]", where [word1] and [word2] are the words entered by the user. Otherwise, simply print: "NO!".