Hello I have this code here what I need to do is count the number of chars in this string. The word can be as long as WORD_LENGTH. While debugging in gdb the string is full of garbage after the chars I set. I tried to malloc the memory but I get the same result. Each word will end with the '\n' as they will be read from a text file. Thank you.
The string is char *word = "Adam\n";
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
int get_word_length(constchar *word)
{
int count = 0;
if (word == NULL)
{
printf ("NULL pointer..\n");
return 0;
}
for(int i = 0; i < WORD_LENGTH; i ++)
{
if (isalpha(word[i]) && (word[i]) != '\n')
{
count++;
}
}
return count;
}