从文本文件中读取并使用每一行来比较它们是否是字谜

时间:2016-10-25 09:14:17

标签: c

我必须修改我的程序以接受来自的输入 一个名为anagrams.txt的文件。该文件每行应该有两个字符串,用#字符分隔。我的程序应该阅读 每对字符串并报告每对字符串是否为字谜。例如,考虑anagrams.txt的以下内容:

你好#elloh
男人#南
天文#Oastrrasd

您的程序应打印出以下内容:

你好#elloh - Anagrams!

man#nam - Anagrams!

Astro#Oastrrasd-不是字谜!

我应该用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;
}

查找文本是否为字谜的代码:

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

3 个答案:

答案 0 :(得分:0)

由于这看起来像是一个大学问题,我不会提供完整的解决方案,只提示。

您所要做的就是将anagram-finding文件的stdin输入部分替换为您从文件中读取的代码:它就像更改

一样简单
printf("Enter the string\n");
gets(array1);
printf("Enter another string\n");
gets(array2);

要     //程序之前:     #define SIZE 1000

// inside main

if (access("anagrams.txt", F_OK) == -1){
    printf("\nFile not found!\n");
    return 1; // Abort the program early if we can't find the file
}
FILE *ptr_file;
char buf[1000];

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

char array1[SIZE], array2[SIZE];

while (fgets(buf, 1000, ptr_file)!=NULL){
    // do all your anagram stuff here!
    // there is currently one line of the input file stored in buf

    // Hint: You need to split buf into array_1 and array_2 using '#' to separate it.

}


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

补充意见:

  1. 不要 使用getsgets没有检查它写入的字符串是否可以保存数据,如果输入的输入大于数组大小,则会导致程序崩溃。请改用fgets(buf, BUF_SIZE, stdin)
  2. 漂亮的代码是很好的代码。如果人们可以轻松阅读您的代码,他们更有可能提供帮助。 (修复你的括号)
  3. 只是为了感兴趣,检查字谜的更有效的算法是使用qsort对两个数组进行排序,然后使用简单的字符串匹配器来比较它们。与O(mnlog(m+n))相比,这将花费O(m^2 n^2),而不是当前算法

答案 1 :(得分:0)

您需要将fgets(正如您所读)读取的每一行拆分为两个字符串,然后将它们传递给find_anagram函数。您可以使用strtok执行此操作:

int main()
{
  int flag;
  char buf[1000];
  FILE *ptr_file;

  //Check file existence

  //Open the file for reading

  while (fgets (buf, 1000, ptr_file) != NULL)
    {
      char *array1 = strtok(buf, "#");
      char *array2 = strtok(NULL, "\n");

      flag = find_anagram (array1, array2);

      //Check flag value to print your message
    }

  return 0;
}

//put your find_anagram function

不要忘记#include <string.h>使用strtok()

答案 2 :(得分:0)

您可以尝试这样的事情:

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