i have a problem using strtok.
i'm trying to write a c program that counts the number of words in a sentence.
i'm using strtok , but for some reason i get an exception while the call for strtok is being provoked.
here is my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
int countWords(char str[]){
int counter;
char * tmp;
counter=0;
printf("the string is: %s\n", str);
tmp = strtok(str," ");
while (tmp != NULL){
counter++;
printf("%s\n", tmp);
tmp = strtok(NULL," ");
}
return counter;
}
and my main:
1 2 3 4 5 6 7
int main(){
int s;
s = countWords2("amir is bla bla bla ");
printf("%d", s);
system("pause");
}
when i define the string in the fumction itself the problem doesent occur.
so i'm guessing the problemis with the pointer i'm sending to the function, but i cant really understand how it is a problem and how do i solve it.
This isn't a C forum, so you might get more help if you posted the question on an appropriate forum.
strtok(NULL,...) looks fishy to me, so you should check if you're invoking strtok correctly: http://www.cplusplus.com/reference/clibrary/cstring/strtok/
thx fot the reply.
i was hopping that ppl with knowledge in c++ will also be able to answear it.
and sceond, strtok(NULL," ") is the right way to invoke the method. (as u can see in the link u added)
The next step (or rather, the first) is to run the debugger to see which line is causing the problem and what the values of the relevant variables are at the time.