我必须修改我的程序以接受来自
名为anagrams.txt的文件。该文件每行应有两个字符串,用字符分隔我的程序应该是
每对字符串,如果每对字符串是一个anagram,则返回报告例如,考虑anagrams.txt的以下内容:
你好#elloh
曼南
阿童木
您的程序应打印出以下内容:
你好#elloh-Anagrams!
男#南-字谜!
阿童木-不是字谜!
我应该编译成g++
下面是从文本中读取的代码:

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;
}

查找文本是否为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;
}

最佳答案

你可以试试这样的方法:

#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;
}

关于c - 从文本文件中读取,并使用每一行进行比较,看看它们是否是字谜,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40235955/

10-10 13:35