Search code examples
cstringoutputsizestrtok

C - issues with sizeof (featuring strtok)


Edit: I later discovered this issue mainly stemmed from my confusion with sizeof , and replacing it with strlen was pretty much my solution. My answer (scroll down) presents a decent but simple example of strtok if you're interested as well.

So I've been trying to get a program working where I input a list of words separated by commas, and it subsequently outputs those words, line by line, and removing any spaces.

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define delim ","

int main() {
    //variable declaration:
    char words[100];
    char *word;
    char tempWord[100];
    int n;

    //gets input assinged to "words":
    puts("\nEnter a list of words separated by commas.\n");
    fgets(words, sizeof(words), stdin);


    //sets up the first word in strtok
    word = strtok(words, delim);

    //loops so long as the word isn't null (reaching the last word)
    while (word != NULL) {
        puts("\n");

        //checks if each character in the word is a space (and ignores them if they are)
        for (n = 0; n < sizeof(word); ++n) {

            //for some reason can't directly use word (probably because it's a pointer)
            //so have to copy it to a temporary value
            strcpy(tempWord, word);

            //don't print if it's a space
            if (!isspace(tempWord[n])) printf("%c", tempWord[n]);
        }

        //moves to next word
        word = strtok(NULL, delim);
    }
    return(0);
}

by inputting "LETS, FREAKINGG, GOOOOOOOOOOOO", I seem to encounter an issue:

(running the program):

Enter a list of words separated by commas.

(input) >>>LETS, FREAKINGG, GOOOOOOOOOOOO


LETS

FREAKIN

GOOOOOO

It seems depending on the size of the first word, it sets an character limit to be no more than 3 beyond that for subsequent words. Can anyone explain why this is happening?


Solution

  • Thank you @rici and @kaylum for the responses!

    For some reason I was getting some errors before when not copying the word to a temporary variable, I'm not entirely sure what I was thinking by putting it inside of the for-loop but it works without! Just a very small brain moment...

    I believe my confusion was around sizeof(), and replacing with strlen() is a complete fix for this!

    Here's the amended (working) code:

    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #define delim ","
    
    int main() {
        //variable declaration:
        char words[100];
        char *word;
        int n;
    
        //gets input assinged to "words":
        puts("\nEnter a list of words separated by commas.\n");
        fgets(words, sizeof(words), stdin);
    
    
        //sets up the first word in strtok
        word = strtok(words, delim);
    
        //loops so long as the word isn't null (reaching the last word)
        while (word != NULL) {
            puts("\n");
    
            //checks if each character in the word is a space (and ignores them if they are)
            for (n = 0; n < strlen(word); ++n) {
    
                //don't print if it's a space
                if (!isspace(word[n])) printf("%c", word[n]);
            }
    
            //moves to next word
            word = strtok(NULL, delim);
        }
        return(0);
    }
    

    and the output:

    Enter a list of words separated by commas.
    
    (input) >>>LETS, FREAKINGG, GOOOOOOOOOOOO
    
    
    LETS
    
    FREAKINGG
    
    GOOOOOOOOOOOO
    

    Thank you very much for the help!