write a function for strtok()??

Answer Posted / manya

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

char * __mstrtok(char *str, char *delimiters)
{
int i;
char map[32];
char *dlmt = delimiters;
char *s1,*s2;
static char *laststr;

for(i=0;i<32;i++)
map[i] = 0;

for(;*dlmt;dlmt++)
map[*dlmt >> 3] |= 1 << (*dlmt & 7);

if(str)
s1 = str;
else
s1 = laststr;

if(!s1)
return NULL;

if(map[*s1 >> 3] & 1 << (*s1 & 7))
s1++;

s2 = s1;

for(;*s1;s1++)
{
if(map[*s1 >> 3] & 1 << (*s1 & 7))
{
*s1++ = '\0';
laststr = s1;
return s2;
}
}

return NULL;
}

int main()
{
char *token;
char string[] = "Hi friend, how are you? How is life! going
on, right.";

for(token=__mstrtok(string," ,?!.");
token;
token=__mstrtok(NULL," ,?!."))
printf("|%s|",token);

printf("\n Done \n");
return 0;
}

Is This Answer Correct ?    3 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is #define in c?

622


Write a C program to count the number of email on text

1420


What is getch c?

856


Is it better to use a macro or a function?

657


What is function and its example?

628






Is there a way to have non-constant case labels (i.e. Ranges or arbitrary expressions)?

583


Give the rules for variable declaration?

678


in linking some of os executables are linking name some of them

1651


Is c++ based on c?

654


a formula,a series of steps,or well defined set of rules for solving a problem a) algorithem b) program c) erdiagram d) compiler

613


int i[2], j; int *pi;i[0] = 1; i[1] = 5; pi = i; j = *pi + 1 + *(pi + 1)Value of j after execution of the above statements will be a) 7 b) 6 c) 4 d) pointer

657


Can a function argument have default value?

674


Is c compiled or interpreted?

667


What is the scope of static variable in c?

535


Are pointers integers in c?

611