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
- #include <stdio.h>
- #include<string.h>
- int main ()
- {
- char str[] ="This a sample string.";
- char * pch;
- printf ("Splitting string \"%s\" into tokens:\n",str);
- pch = strtok (str," ,.-");
- while (pch != NULL)
- {
- printf ("%s\n",pch);
- pch = strtok (NULL, " ,.-");
- }
- return 0;
- }
Expand|Select|Wrap|Line Numbers
- #include<stdio.h>
- void main()
- {
- char str[20];
- int i = 0,length;
- printf("Enter any sentence\n");
- gets(str);
- printf("the sentence you entered is : %s\n",str);
- length = strlen(str);
- printf("string length is %d \n",length);
- printf("seperating the sentence into different strings\n");
- while(i < length)
- {
- if(str[i] != ' ')
- {
- printf("%c",str[i]);
- i++;
- }
- else
- {
- printf("\n");
- i++;
- }
- }
- printf("\n");
- return;
- }
Thank you in advance.