By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 1,686 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

Can anyone help me to understand the strtok() function.

P: 6
Hi, I just confused with strtok() functionality.
while Iam trying to know about strtok() function, I found a sample code in one of the C websites. After executing that code what i come to know was "the string which we are giving" was separated and printed as a separate strings. The below code is that reference code.
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include<string.h>
  3. int main ()
  4. {
  5.   char str[] ="This a sample string.";
  6.   char * pch;
  7.   printf ("Splitting string \"%s\" into tokens:\n",str);
  8.   pch = strtok (str," ,.-");
  9.   while (pch != NULL)
  10.   {
  11.     printf ("%s\n",pch);
  12.     pch = strtok (NULL, " ,.-");
  13.   }
  14.   return 0;
  15. }
After that I want to separate the string without using strtok() function. So That i implement a code like this.
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. void main()
  3. {
  4.     char str[20];
  5.     int i = 0,length;
  6.     printf("Enter any sentence\n");
  7.     gets(str);
  8.     printf("the sentence you entered is : %s\n",str);
  9.     length = strlen(str);
  10.     printf("string length is %d \n",length);
  11.     printf("seperating the sentence into different strings\n");
  12.     while(i < length)
  13.     {
  14.         if(str[i] != ' ')
  15.         {
  16.             printf("%c",str[i]);
  17.             i++;
  18.         }
  19.         else
  20.         {
  21.             printf("\n");
  22.             i++;
  23.         }
  24.     }
  25.     printf("\n");
  26.     return;
  27.  
Actually my doubt is Iam getting the output same as like previous code(strtok()). But, my trainer said that my code is totally wrong. So anyone please let me know what does internally happening in the strtok() function and what's wrong in my code.
Thank you in advance.
Sep 23 '15 #1

✓ answered by donbock

strtok treats any and all characters in second argument as delimiters. The second program only recognizes the space character as a delimiter.

strtok treats a series of consecutive delimiter characters as a single delimiter. The second program doesn't.

strtok ignores leading delimiters. The second program doesn't.

A couple of unrelated comments...
Always define main as returning int, never void.
gets is deprecated because it invites buffer overflow problems. Don't use it.

Share this Question
Share on Google+
2 Replies


Expert 100+
P: 2,396
strtok treats any and all characters in second argument as delimiters. The second program only recognizes the space character as a delimiter.

strtok treats a series of consecutive delimiter characters as a single delimiter. The second program doesn't.

strtok ignores leading delimiters. The second program doesn't.

A couple of unrelated comments...
Always define main as returning int, never void.
gets is deprecated because it invites buffer overflow problems. Don't use it.
Sep 23 '15 #2

P: 6
Thank u Sir for your Explanation.
I have a small doubt.
Is strtok() function Separating that Sentence into multiple strings or it just printing them as seperately ???
Incase of it is seperating it as a multiple strings, Where these strings are storing and how??
Thanks you..
Sep 24 '15 #3

Post your reply

Sign in to post your reply or Sign up for a free account.