user784637
user784637

Reputation: 16152

How to correctly null terminate a c string?

I have the following code which counts the number of occurrences of each unique term in a text document. I believe I correctly terminated each c-string with '\0'

#include <stdio.h>
#include <string.h>
int main ()
{
    int c;
    FILE *file;
    int NUMBER_OF_WORDS = 100;
    int MAX_WORD_LENGTH = 30;

    char uniqueWords[NUMBER_OF_WORDS][MAX_WORD_LENGTH+1];
    int wordCount[NUMBER_OF_WORDS];
    int uniqueWordIndex =0;

    char tempWord[MAX_WORD_LENGTH+1];
    int tempWordIndex = 0;

    file = fopen("sample.txt", "r");
    if (file) {
        while ((c = getc(file)) != EOF && uniqueWordIndex < 100){
            if( isalpha(c)){
                tempWord[tempWordIndex] = c;
                tempWordIndex++;
            }else if ( (c == ' ' || c == '\n') && strlen(tempWord) > 0  ) {
                tempWord[tempWordIndex] = '\0';
                int k = 0;
                int newUnique = 1;
                for (k=0; k<NUMBER_OF_WORDS; k++){
                    if (strcmp (tempWord, uniqueWords[k]) == 0){
                        wordCount[k]++;
                        newUnique = 0;
                        break;
                    }
                }
                if (newUnique){
                    int i=0;
                    wordCount[uniqueWordIndex] = 1;
                    for (i=0; i<strlen(tempWord); i++)
                        uniqueWords[uniqueWordIndex][i] = tempWord[i];
                    uniqueWords[uniqueWordIndex][i] = '\0';
                    uniqueWordIndex++;
                }

                tempWordIndex = 0;

            }
        }
        int i =0;
        for (i =0; i< NUMBER_OF_WORDS; i++){
            int k = 0;
            for (k =0; k< strlen(uniqueWords[i]); k++)
                printf("%c",uniqueWords[i][k]);
                printf(" %d\n", wordCount[i]);
        }
        fclose(file);
    }
    return(0);
}

Is there any syntax error that's resulting in wacky output like this?

term 2
something 5
reading 1
level 1
!J<8F><FF>^? 0
<C8>B~8<91>^? 0

Upvotes: 0

Views: 160

Answers (1)

Nigel
Nigel

Reputation: 354

It looks like you are not guaranteed to have NUMBER_OF_WORDS entries in uniqueWords or wordCount, but you are printing out that many at the end. Whether or not that is responsible for the output you are seeing, it would be likely to produce such output if your input has less than NUMBER_OF_WORDS unique words.

Upvotes: 3

Related Questions