473,387 Members | 1,572 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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.

2 1482
donbock
2,426 Expert 2GB
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
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

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

Similar topics

1
by: Paul Brun | last post by:
Hello all, Well....I debugged in application further (see int32 to native int thread) and decided to make a new post as it is easier for trackability... Anyway, I am getting a Null Reference...
3
by: Franck | last post by:
Hello, I'm looking for a way of giving some arguments to the client's printer I've found that link but...
8
by: Siv | last post by:
Hi, Does anyone have a function that when called returns which operating system we are running on. E.g: Select Case GetOSVer() Case 1 'Win95 ... Case 2 'Win98 ... Case 3 'Win98SE
4
by: default | last post by:
Hello All. I was looking around for a function like strtok() which would tokenize on the complete list of delimiters, rather than tokenize on *any* of the delimiters in the group. I ended up...
2
by: Sean Hammond | last post by:
Anyone understand this? Python 2.4.4c1 (#2, Oct 11 2006, 21:51:02) on linux2 Type "help", "copyright", "credits" or "license" for more information. .... """Send 'input' (string) to the...
9
by: Chris Saunders | last post by:
I do know C but am not an expert. I'm creating an interface to the GLUT library from the language Eiffel. Here is the signature for a function that I do not understand: GLUTAPI void APIENTRY...
15
by: Neil | last post by:
So, I tried to copy a toolbar. I have a custom menu bar for a form. Tried to copy it so that I could modify the copy and use it with another form. With no direct way to copy it, I made a copy of...
0
by: kepbem | last post by:
I am new to this forum, i have created this database, however the timetables will not reset themselves. They have done previously, can anyone help? Option Compare Database Option Explicit ...
1
by: Man4ish | last post by:
Hi, I am parsing one tab delimited file with some missing values. When i use the strtok function, I can not get the values based on index in vector. The code shown below explains my pblm. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.