特殊输入的基本字数统计测试

问题描述 投票:0回答:1

目标。
该程序将从用户处读取一行文本并打印出每个单词在输入中出现的次数。

要求。

  • 将字符串更改为全部小写字母to_lowercase(char *str)
  • 循环字符串并将所有字母更改为小写
  • 搜索已找到的单词:
    find_word(char words[][MAX_WORD_LENGTH], int size, char *word)
  • 搜索已找到的所有单词并查看是否与当前单词匹配
  • 如果找到匹配项,则返回匹配项的索引
  • 如果没有找到匹配则返回-1

主要功能。

  • 提供的输入(用于用户输入)、found_words(保存找到的单词的矩阵)、counts(保存索引处计数的数组)
  • 提示用户输入句子
  • 使用 fgets 通过标准输入接收句子
  • 如果输入的最后一个字符是换行符,则将其删除
  • 使用strtok根据空格(“”)对输入进行标记
  • 当strtok找到新单词时,继续循环句子中的单词
  • 使用当前单词和找到的单词调用 find_words
  • 如果返回为-1,则将当前单词复制到found_words数组并增加最大索引的计数
  • 如果返回不为-1,则增加相关的计数值
  • 增加字数
  • 打印出每个单词的计数

完成步骤。

  • 落实要求
  • 编译并测试程序
  • 创建1个用户测试用例,使用测试用例中的描述来指导测试用例的创建。
  • 运行/挑战/测试器
  • 获取旗帜

main.c:

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

#define MAX_WORDS 100
#define MAX_WORD_LEN 50

// Helper function to convert a string to lowercase
void to_lowercase(char *str) {
    for (int i = 0; str[i]; i++) {
        str[i] = tolower(str[i]);
    }
}

// Check if word already exists in the list
int find_word(char words[][MAX_WORD_LEN], int word_count, char *word) {
    for (int i = 0; i < word_count; i++) {
        if (strcmp(words[i], word) == 0) {
            return i; // return the index of the word if found
        }
    }
    return -1; // return -1 if not found
}

int main() {
    char input[1000];
    char words[MAX_WORDS][MAX_WORD_LEN];
    int counts[MAX_WORDS] = {0};
    int word_count = 0;

    // Prompt the user for input
    printf("Enter a line of text: ");
    fgets(input, sizeof(input), stdin);

    // Remove the newline character at the end of the input if it exists
    size_t len = strlen(input);
    if (len > 0 && input[len - 1] == '\n') {
        input[len - 1] = '\0';
    }

    // Tokenize the input by spaces
    char *token = strtok(input, " ");
    while (token != NULL) {
        to_lowercase(token); // convert token to lowercase

        int index = find_word(words, word_count, token);

        if (index == -1) {
            // New word, add it to the list
            strcpy(words[word_count], token);
            counts[word_count]++;
            word_count++;
        } else {
            // Existing word, increment its count
            counts[index]++;
        }

        token = strtok(NULL, " ");
    }

    // Output the word counts
    printf("word counts:");
    for (int i = 0; i < word_count; i++) {
        printf(" %s: %d", words[i], counts[i]);
        if (i < word_count - 1) {
            printf(" "); // Space between words
        }
    }
    printf("\n");

    return 0;
}
---------------[  User Tests  ]---------------
User utest5.2.1: ✔ PASS  - Basic word counting test with special input ran in 0.01s
User utest5.2.1: FAIL  - Basic word counting test with special input 
        Test Desc        : Tests word counting with special input 'Hello world Hello\n\n' and case insensitivity
        Command          : /challenge/modelGood.bin 
        Program Input    : 'Hello world Hello\n\n\n'
        Expected Output  : ['word counts: hello: 2 world: 1\n']
        Test ran in      : 0.01s
        Failed to find in output   : word counts: hello: 2 world: 1
        Closest match is at line 2 : hello: 2
--------<[ utest5.2.1   (marked output of /tmp/test_output/utest5.2.1_actual_normalized.output) ]>--------
1  enter a line of text: word counts:
2  hello: 2
         ↑↑↑↑↑
3  world: 1
↑ EOF ↑

User utest5.2.1: ✔ PASS  - Basic word counting test with special input ran in 0.01s

---------------[ System Tests ]---------------
System stest5.2.1: ✔ PASS  - Test for the counts for the above input ran in 0.01s
System stest5.2.2: ✔ PASS  - Test input for the expected counts  ran in 0.01s

Summary: 4 tests passed, 1 tests failed
Too many failures to receive flag

我无法解决此错误:

User utest5.2.1: FAIL  - Basic word counting test with special input 
        Test Desc        : Tests word counting with special input 'Hello world Hello\n\n' and case insensitivity
        Command          : /challenge/modelGood.bin 
        Program Input    : 'Hello world Hello\n\n\n'
        Expected Output  : ['word counts: hello: 2 world: 1\n']
        Test ran in      : 0.01s
        Failed to find in output   : word counts: hello: 2 world: 1
        Closest match is at line 2 : hello: 2
--------<[ utest5.2.1   (marked output of /tmp/test_output/utest5.2.1_actual_normalized.output) ]>--------
1  enter a line of text: word counts:
2  hello: 2
         ↑↑↑↑↑
3  world: 1
↑ EOF ↑
c
1个回答
0
投票

我无法解决此错误:

有一件事看起来确实很奇怪“程序将从用户那里读取一行文本”以及

special input 'Hello world Hello\n\n'

特殊输入看起来像2行。

代码只能读取 1 行。 而是读取多行

// fgets(input, sizeof(input), stdin);

while (fgets(input, sizeof(input), stdin) != NULL) {
  ...
}

然后打印输出。

© www.soinside.com 2019 - 2024. All rights reserved.