1
#include <stdio.h>
#include <string.h>

int main(void) {

  char tekst[10000], test=0;
  char* word;
  char word_copy[100][100];
  int i=0, lenght=0;

  printf("Type in your text: ");
  fgets(tekst, 10000, stdin);

  lenght=strlen(tekst)-1;
  if(lenght>1000)
  {
    lenght=1000;
  }

  word=strtok(tekst, " ,\".-");

  while( word != NULL)
    {
      word=strtok(NULL, " ,\".-");
      printf("%s ", word);
      i++;

    }

  printf("%d", i);

Hello. What I want to do, is to count words only by using strtok. However, if i type: "example" or -example-, i get the answer "2" instead of "1". For some reason, when the last word is (null), it still triggers the loop, and i++ works... I'm pretty new to programming, so i would aprreciate writing the correct code down aswell.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
Nettle
  • 31
  • 1
  • 5
  • don't try to print `NULL` for starters. Test if NULL before printing it. – Jean-François Fabre Nov 27 '18 at 18:39
  • and break when strtok return NULL so you don't count it. – Jean-François Fabre Nov 27 '18 at 18:42
  • 3
    It's not too early to learn how to use a debugger to figure out what's happening in your program. – John Bollinger Nov 27 '18 at 18:45
  • You count `word` even when it's null. Use `while ((word = strtok(NULL, " ,\".-")) != NULL) { printf("%s ", word); i++; }`. – Jonathan Leffler Nov 27 '18 at 18:45
  • Actually, I know i should use strcpy or test it before printing, but I was confused and needed some quick information back and left it here while copying, haha. And thank you very much. I've tried breaking the while earlier, but I was using "word" in the if, and this time I've tried full "strtok(..)", as suggested, and it worked! I really appreciate your help. Have a nice day! – Nettle Nov 27 '18 at 18:47

2 Answers2

2

Continuing from the comments, here's one more way to fix your code:

...
char *text = tekst;
while((word = strtok(text, " ,\".-"))) {
    text = NULL;
    printf("%s ", word);
    i++;
}
...
DYZ
  • 55,249
  • 10
  • 64
  • 93
  • 1
    GCC will warn about the assignment in a conditional with very little provocation (`-Wall` etc). I'd write `while ((word = strtok(text, " ,\".-")) != NULL)` to avoid that warning — at no cost in the generated code. – Jonathan Leffler Nov 27 '18 at 19:18
  • @JonathanLeffler I was surprised that gcc 5.4.0 did not issue that warning (with -Wall and -pedantic). – DYZ Nov 27 '18 at 19:52
  • The extra parentheses are a symbol to GCC that you know what you're doing. (Otoh, MSVC complains regardless.) – Neil Nov 27 '18 at 20:23
0

You want to remove the trailing newline of fgets(), like mentioned in Removing trailing newline character from fgets() input.

Then, I removed the not used stuff of your code, like length.

As the example in strtok()'s reference mentions, you first take the token, and then you call the function again. With that approach, you don't need to worry about counting NULL as a word (which your code did).

Putting everything together, we get:

#include <stdio.h>
#include <string.h>

int main(void) {

  char tekst[10000];
  char* word;
  int i=0;

  printf("Type in your text: ");
  fgets(tekst, 10000, stdin);
  tekst[strcspn(tekst, "\n")] = 0;

  word=strtok(tekst, " ,\".-");

  while( word != NULL)
  {
      printf("%s ", word);
      word=strtok(NULL, " ,\".-");
      i++;

  }

  printf("%d", i);

  return 0;
}

Output (Input: "stack-overflow"):

Type in your text: stack overflow 2


PS: strlen() returns the length of the string without the null terminator, no need to subtract by yourself.

gsamaras
  • 71,951
  • 46
  • 188
  • 305