比较中间有多个空格的字符串

时间:2014-03-26 23:48:40

标签: c strcmp strcat

我有一个编程任务,我应该让一个程序将一个短语翻译成一个虚构的语言,然后将它与测试翻译进行比较,以确认它是否正确完成。我可以让程序正常工作,除非翻译之前的输入在单词之间有多个空格,我应该忽略它。我翻译字符串,只在单词之间放置一个空格(无论单词之间的输入有多少),并将其与测试翻译进行比较以确保它有效。这是我的职能......

// Tests for taurahize_word ----------------------------------------------------
#define NBPHRASES 4
#define PHRASES               { {"a aa aaa", "A Ba Aki"},\
                            {"aaa    aaa", "Aki Aki"},\
                            {"aaaa aaa", "Aoke Aki"},\
                            {"Where is everyone", "Aehok Ba Akiticha"}\
                            };


char* taurahize_phrase(const char* const phrase){
    //allocates memory for string array "translation" using length of string to determine size
    int length = strlen(phrase);
    char* translation = (char*)malloc(sizeof(char) * length);
    *translation = 0;

    //copy of original phrase given to workingTranslation
    char* workingTranslation = strdup(phrase);

    //workingTranslation is tokenized into "transTokens" array
    char* transTokens = strtok(workingTranslation, " ");

    while (transTokens != NULL) {
        char* tempTranslation = taurahize_word(transTokens);
        transTokens = strtok(NULL, " ");
        strcat(translation, tempTranslation);
        strcat(translation, " ");
    }

    *(translation + length) = '\0';

    free(workingTranslation);
    free(transTokens);

    return strdup(translation);
}

void runMultipleWordsTests(){
     /* ROLE       Runs tests on the taurahize_phrase function
     */
     int n = 0;
     char* translation = NULL;
     const char* phrases[NBPHRASES][2] = PHRASES;

     printf("\n\n\nTESTING PHRASES TRANSLATIONS\n");

     for(n = 0; n < NBPHRASES ; n++) {
        translation = taurahize_phrase(phrases[n][0]);
          if(strcmp(translation, phrases[n][1]) == 0)
              printf("Test #%d\t%s\n", n, "OK");
          else
              printf("Test #%d\t%s\n", n, "FAILED");

          #ifdef VERBOSE_TESTING
          printf("\tinput is \t%s\n", phrases[n][0]);
          printf("\texpected is \t%s\n", phrases[n][1]);
          printf("\tobserved is \t%s\n", translation);
          #endif
          free(translation);
     }
}

char* taurahize_word(const char * const word){
    // constant string array with all of the possible translations
    static const char *TRANSLATIONS[16] = {"", "A", "Ba", "Aki", "Aoke", "Aehok", "Aloaki", "Ishnelo", "Akiticha",
                                    "Echeyakee", "Awakahnahe", "Aloakehshni", "Awakeekieloh", "Ishnehawahalo", "Awakeeahmenalo",
                                    "Ishnehalohporah"};

    //determine length of passed word
    int length = strlen(word);

    //temporary string to hold translation before returning
    const char* tempWord;

    //default return if the word is too long to translate
    if (length > 15) {
        tempWord = "#@%";
        return strdup(tempWord);
    }

    //assigns string translation to string "tempWord"
    tempWord = TRANSLATIONS[length];

    //returns translation
    return strdup(tempWord);
}

void runMultipleWordsTests函数是遗留代码,不应修改,我的taurahize_word函数除了多个单词test之外的所有内容都有效,并且单词之间有多个空格。这有什么明显的理由呢?我认为在strcmp中使用runMultipleWordsTests时,它与终止字符有关。我在strcat中使用taurahize_phrase来构造字符串(正如我们的说明所说),但我也再次使用该函数在每个单词后面添加一个空格。这些比较都没有通过。考虑终止字符可能是问题,我使用行*(translation + length) = '\0'将最终字符转换为null,但这只解决了单个空格的短语问题。

测试输出......

Taurahize Test Output

2 个答案:

答案 0 :(得分:1)

更改

char* translation = (char*)malloc(sizeof(char) * length);

通过

char* translation = (char*)malloc(sizeof(char) * (length + 1));

编辑:

我想我明白了^^

更改:

while (transTokens != NULL) {
        char* tempTranslation = taurahize_word(transTokens);
        transTokens = strtok(NULL, " ");
        strcat(translation, tempTranslation);
        strcat(translation, " ");
    }

int i = 0;
while (transTokens != NULL) {
        if (i++ != 0)
         strcat(translation, " ");
        char* tempTranslation = taurahize_word(transTokens);
        transTokens = strtok(NULL, " ");
        strcat(translation, tempTranslation);
    }

说明:

这是一个循环的例子。假设你的句子由3个单词组成(所以3变成while循环)

转1

我等于0 - &gt;不要添加&#39; &#39;翻译 将WORD1添加到翻译中

翻译等于&#34; WORD1&#34;在turn1的末尾

转2

我等于1 - &gt;添加&#39; &#39;翻译 将WORD2添加到翻译中

翻译等于&#34; WORD1 WORD2&#34;在turn2结束时

转3

我等于2 - &gt;添加&#39; &#39;翻译 将WORD3添加到翻译中

翻译等于&#34; WORD1 WORD2 WORD3&#34;在turn3结束时

然后退出循环

你的问题是:为什么所有时间都等于真?

  • 不是!我在循环的第一个回合中等于0

我改变了初始算法,解释:

  • 在此之前,&#34;添加单词并添加空格&#34;
  • 现在是,&#34;如果我旁边有一个单词,那么添加空格,然后添加单词&#34;

答案 1 :(得分:1)

考虑当您到达输入短语的最后一个标记时会发生什么。你最后在最后一个单词的末尾添加了一个额外的空格。

如何检查这一点,以及在这种情况下你应该怎么做?