سؤال

I've got a programming assignment where I'm supposed to make a program translate a phrase into a fictional language, and then compare it with test translations to confirm it was done correctly. I can get the program to work fine unless the input before translating has multiple spaces between words, which I'm supposed to ignore. I translate the string, only put a single space between words (regardless of how many were in the input between words), and compare it with the test translation to make sure it works. Here are my functions...

// 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);
}

The void runMultipleWordsTests function is legacy code and isn't supposed to modified, and my taurahize_word function has worked for everything besides the multiple words test with more than one space between words. Any obvious reasons it would be doing this? I think it has something to do with the terminating character when strcmp is used in runMultipleWordsTests. I use strcat in taurahize_phrase to construct the string (as our instructions say), but I also use the function again to add a space after each word. None of the comparisons were passing. Thinking the terminating character might be the problem, I made the line *(translation + length) = '\0' to convert the final character to null, but that only solved the issue for phrases with single spaces.

Test output...

Taurahize Test Output

هل كانت مفيدة؟

المحلول

Change

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

by

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

EDIT:

I think i get it^^

Change :

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

to

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

Explanation :

Here is an exemple of loop. Assume that your sentence is composed of 3 words (so 3 turn into while loop)

Turn 1:

i equals 0 -> DO NOT ADD ' ' into translation ADD WORD1 into translation

translation equals "WORD1" at the end of the turn1

Turn 2:

i equals 1 -> ADD ' ' into translation ADD WORD2 into translation

translation equals "WORD1 WORD2" at the end of the turn2

Turn 3:

i equals 2 -> ADD ' ' into translation ADD WORD3 into translation

translation equals "WORD1 WORD2 WORD3" at the end of the turn3

then quit the loop

Your question is : why the if is all time equals to true?

  • it is not! i is equals to 0 in the first turn of the loop

I change the initial algorithm, explain :

  • Before that was, "Add the word and add a space"
  • Now it is, "If there is a word beside me so add space, then add word"

نصائح أخرى

Consider what happens when you reach the last token of your input phrase. You end up adding an extra space at the end of the last word.

How can you check for this, and what should you do in this case?

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top