简体   繁体   中英

Tokenizer programme in C

I just started to learn how to programme in C and I was asked to code a tokenizer programme, which splits up the words in a sentence into different tokens, basically what strtok() does.

#include <stdio.h>
#include <stdlib.h>
int tokenise(char str[], int start, char result[]);
int main(){
    const int MAX_STRING = 256;
    char buffer[MAX_STRING];
    int start;
    int count;
    int i;
    char result[MAX_STRING];
    fgets(buffer, MAX_STRING, stdin);
    printf("%s\n", buffer);
    start = tokenise(buffer, 0, result);
    while(start != -1){
    printf("%s\n", result);
        start = tokenise(buffer, start, result);
}
int tokenise(char str[], int start, char result[])
{
    int j;
    for( i = start; str[i] != ' '; i++)
    {
       result[j] = str[i];
       j++;
    }
    j = 0;
   return -1;
}
}

This is the code I have so far and I don't understand why my function doesn't work. Is there something basic I did wrong or have I got something massively wrong? I am also confused as to why my lecturer has

start = tokenise(buffer, 0 , result);

right above the while loop. Any help is appreciated, thank you.

  • You always return -1 no matter the outcome of the function.
  • You don't look for line feed '\\n' and other such white space characters. Consider using the isspace() function in ctype.h.
  • You don't look for the end of the string, '\\0' , meaning that if there are no spaces in the string, your program will crash.

Other than problems Lundin mentioned in his answer ,

In function int tokenise(char str[], int start, char result[])

int j;

j is uninitialized , but then also incremented in loop . Initialzed j to 0 before using it otherwise it will have indeterminate value.

Also due to wrong placement of { } your function tokenize is defined inside main . Define it outside main , which is usually done.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM