write a function for strtok()??

Answers were Sorted based on User's Feedback



write a function for strtok()??..

Answer / 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

write a function for strtok()??..

Answer / yogesh

strtok() function is used for tokanizing the given string.
The output of the following program will be as follows.
How|are|you|I|am|Fine.

#include<stdio.h>
int main()
{
char *p;
char str[40]="How are you,I am Fine";
p=strtok(str," ");
printf("%s",p);
do
{
p=strtok('\0',", ");
if(p)
printf("|%s",p);
}while(p);
printf("\n");
}

Is This Answer Correct ?    4 Yes 5 No

Post New Answer

More C Interview Questions

Differentiate Source Codes from Object Codes

1 Answers  


hi any body pls give me company name interview conduct "c" language only

0 Answers  


How do you view the path?

0 Answers  


write a program that reads lines(using getline), converts each line to an integer using atoi, and computes the average of all the numbers read. also compute the standard deviation.

0 Answers  


What is const keyword in c?

0 Answers  






Explain what will the preprocessor do for a program?

0 Answers  


c program to manipulate x=1+3+5+...+n using recursion

2 Answers   Wipro,


pascal triangle program

2 Answers  


How we can insert comments in a c program?

0 Answers  


write a program that eliminates the value of mathematical constant e by using the formula e=1+1/1!+1/2!+1/3!+

1 Answers   Reliance,


what is the defrenece between structure and union

5 Answers   Aloha Technology,


why use "return" statement a) on executing the return statement it immediately transfers the control back to the calling program b) it returns the value present in the parentheses return, to the calling program c) a & b d) none of the above

0 Answers  


Categories