0

I am very close to getting my dictionary to word correctly, but I am having a small hiccup in my check function. In order to compare the word given in the check function to the dictionary, I first want to copy the given word in lowercase (because the dictionary entries are all lowercase). As I do this, I am running into a strange problem where I copy 3 letters successfully, but on the fourth letter my new_word char* gets the characters "rgy" appended. Here is the code I am dealing with.

char* new_word = malloc((LENGTH+1) * sizeof(char));
int length = strlen(word);
for (int i = 0; i < length; i++)
    new_word[i] = tolower(word[i]);

Also, here is a link to a picture of my gdb as I run through this code on the word "AUSTIN" as an example of what is happening. http://ibin.co/2A6FgqqDTh9O

3
  • I think that has to do because your string is not null terminated so the program doesn't know up to where to read, so it reads on. If on every iteration you put a \0 at the next char, even though it will take more time, you will see the result you want. On the other hand, if you append the \0 at the end of the whole word, the word should be correctly assigned. – ChrisG Jul 27 '15 at 18:23
  • You were absolutely right. Thank you! – Daniel Smith Jul 27 '15 at 22:40
  • how to append \0 at the end of the whole word....please help me. – Indranee Saha Jun 13 '17 at 4:43

You must log in to answer this question.

Browse other questions tagged .