You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Exercism C语言Isogram挑战本地正常但在线测试失败求助

Exercism C语言Isogram挑战问题排查

我近期在Exercism平台学习C语言,正在完成Isogram(等字母词)挑战。该挑战要求判断单词或短语是否为Isogram:字母无重复,但空格和连字符可重复出现。

共有15个测试用例,其中test_isogram_with_duplicated_hyphen用例在线测试未通过,但本地运行正常。我猜测第15个测试用例可能因未发现的错误意外通过。

我的解决方案分为两步:

  • 第一步:将输入转换为纯小写字母
  • 第二步:逐字母复制到新容器,复制前检查是否重复

以下是测试代码和我的实现代码,希望能找出在线测试失败的原因:

测试代码

#include "test-framework/unity.h"
#include "isogram.h"
#include <stdlib.h>
void setUp(void)
{
}
void tearDown(void)
{
}
static void test_empty_string(void)
{
   TEST_ASSERT_TRUE(is_isogram(""));
}
static void test_null(void)
{
   TEST_IGNORE();   // delete this line to run test
   TEST_ASSERT_FALSE(is_isogram(NULL));
}
static void test_isogram_with_only_lower_case_characters(void)
{
   TEST_IGNORE();
   TEST_ASSERT_TRUE(is_isogram("isogram"));
}
static void test_word_with_one_duplicated_character(void)
{
   TEST_IGNORE();
   TEST_ASSERT_FALSE(is_isogram("eleven"));
}
static void test_word_with_one_duplicated_character_from_end_of_alphabet(void)
{
   TEST_IGNORE();
   TEST_ASSERT_FALSE(is_isogram("zzyzx"));
}
static void test_longest_reported_english_isogram(void)
{
   TEST_IGNORE();
   TEST_ASSERT_TRUE(is_isogram("subdermatoglyphic"));
}
static void test_word_with_duplicated_letter_in_mixed_case(void)
{
   TEST_IGNORE();
   TEST_ASSERT_FALSE(is_isogram("Alphabet"));
}
static void test_word_with_duplicated_letter_in_mixed_case_lowercase_first(void)
{
   TEST_IGNORE();
   TEST_ASSERT_FALSE(is_isogram("alphAbet"));
}
static void test_hypothetical_isogrammic_word_with_hyphen(void)
{
   TEST_IGNORE();
   TEST_ASSERT_TRUE(is_isogram("thumbscrew-japingly"));
}
static void
test_hypothetical_word_with_duplicated_character_following_hyphen(void)
{
   TEST_IGNORE();
   TEST_ASSERT_FALSE(is_isogram("thumbscrew-jappingly"));
}
static void test_isogram_with_duplicated_hyphen(void)
{
   TEST_IGNORE();
   TEST_ASSERT_TRUE(is_isogram("six-year-old"));
}
static void test_made_up_name_that_is_an_isogram(void)
{
   TEST_IGNORE();
   TEST_ASSERT_TRUE(is_isogram("Emily Jung Schwartzkopf"));
}
static void test_duplicated_character_in_the_middle(void)
{
   TEST_IGNORE();
   TEST_ASSERT_FALSE(is_isogram("accentor"));
}
static void test_same_first_and_last_characters(void)
{
   TEST_IGNORE();
   TEST_ASSERT_FALSE(is_isogram("angola"));
}
static void test_word_with_duplicated_character_and_with_two_hyphens(void)
{
   TEST_IGNORE();
   TEST_ASSERT_FALSE(is_isogram("up-to-date"));
}
int main(void)
{
   UnityBegin("test_isogram.c");
   RUN_TEST(test_empty_string);
   RUN_TEST(test_null);
   RUN_TEST(test_isogram_with_only_lower_case_characters);
   RUN_TEST(test_word_with_one_duplicated_character);
   RUN_TEST(test_word_with_one_duplicated_character_from_end_of_alphabet);
   RUN_TEST(test_longest_reported_english_isogram);
   RUN_TEST(test_word_with_duplicated_letter_in_mixed_case);
   RUN_TEST(test_word_with_duplicated_letter_in_mixed_case_lowercase_first);
   RUN_TEST(test_hypothetical_isogrammic_word_with_hyphen);
   RUN_TEST(test_hypothetical_word_with_duplicated_character_following_hyphen);
   RUN_TEST(test_isogram_with_duplicated_hyphen);
   RUN_TEST(test_made_up_name_that_is_an_isogram);
   RUN_TEST(test_duplicated_character_in_the_middle);
   RUN_TEST(test_same_first_and_last_characters);
   RUN_TEST(test_word_with_duplicated_character_and_with_two_hyphens);
   return UnityEnd();
}

我的实现代码

#include "isogram.h"

#include <stdlib.h>
#include <stdio.h>

/* gives new char array from input, only small letters */
char* clean_words(char* ptr_input)
{
    // new container; everything bigger than 27 is impossible to be an isogram
    static char cleaned[27] = {"00000000000000000000000000"};
    int i = 0, j = 0, k = 0;
    for (int i = 0; ptr_input[i] != '\000'; i++)
    {
        k++;
    }
    for (i=0; i <= k; i++, j++)
    {
        if (ptr_input[i] > 64 && ptr_input[i] < 91)
        {
            cleaned[j] = ptr_input[i] + 32;
        }
        else if (ptr_input[i] > 96 && ptr_input[i] < 123)
        {
            cleaned[j] = ptr_input[i];
        }
        else
        {
            j--;
        }
    }
    char* ptr_output = &cleaned[0];
    return ptr_output;
}

bool is_isogram(char phrase[])
{
    if(phrase == NULL) 
        { return false; }
    char* ptr_a = clean_words(phrase);
    char ca_empty[27] = {"00000000000000000000000000"};

    for(int i = 0; i <= 27; i++, ptr_a++){
        // first element is always copied
        if(i == 0){
                ca_empty[i] = *ptr_a;
                continue;
        }
        // '0' is sentinel, meaning the input word is finished => exit loop.
        if(*ptr_a == '0' || *ptr_a == '\000') 
            { break; }
        // following elements only copied if different than current input char
        int j = 0;
            // loop copied for doubles, exit when found
        for(;j<i;j++)
        {
            if(ca_empty[j] == *ptr_a){
                return false;
            }
        }
        // if none was found, copy new letter
        ca_empty[i] = *ptr_a;
    }
    return true;
}

内容的提问来源于stack exchange,提问作者angularNoob

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 10:05:22