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

what is level of tree if leaf node is at level 4.please explain.

1 Answers   Wipro,


An array name contains base address of the array. Can we change the base address of the array?

4 Answers   NMIMS, Wipro,


what is difference b/w extern & volatile variable??

6 Answers   Teleca,


Given a valid 24 hour format time find the combination of the value and write a program ,do not hard the value and if any other inputs provided should work with the logic implemented Input: 11:30 Output: 13:10 Input: 18:25 Output: 21:58

0 Answers   Zoho,


how can i get output the following... 5 4 3 2 1 4 3 2 1 3 2 1 2 1 1 and 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 plz plz...

3 Answers  






`write a program to display the recomended action depends on a color of trafic light using nested if statments

0 Answers  


How can I change their mode to binary?

0 Answers  


The C language terminator is a.semicolon b.colon c.period d.exclamation mark

6 Answers   TCS,


What does double pointer mean in c?

0 Answers  


What is the use of ?: Operator?

0 Answers  


1. Write the function int countchtr(char string[ ], int ch); which returns the number of times the character ch appears in the string. Example, the call countchtr(“She lives in NEWYORK”, ‘e’) would return 3.

4 Answers  


Write a simple code fragment that will check if a number is positive or negative.

0 Answers  


Categories