Reputation: 73
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.
Upvotes: 0
Views: 88
Reputation: 16607
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.
Upvotes: 1
Reputation: 213950
-1
no matter the outcome of the function.'\n'
and other such white space characters. Consider using the isspace()
function in ctype.h.'\0'
, meaning that if there are no spaces in the string, your program will crash.Upvotes: 1