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"