目标。
该程序将从用户处读取一行文本并打印出每个单词在输入中出现的次数。
要求。
find_word(char words[][MAX_WORD_LENGTH], int size, char *word)
主要功能。
完成步骤。
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 ↑
我无法解决此错误:
有一件事看起来确实很奇怪“程序将从用户那里读取一行文本”以及
special input 'Hello world Hello\n\n'
。
特殊输入看起来像2行。
代码只能读取 1 行。 而是读取多行
// fgets(input, sizeof(input), stdin);
while (fgets(input, sizeof(input), stdin) != NULL) {
...
}
然后打印输出。