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

a c code by using memory allocation for add ,multiply of sprase matrixes

0 Answers  


Explain what?s happening in the first constructor: public class c{ public c(string a) : this() {;}; public c() {;} } How is this construct useful?

1 Answers  


Struct(s) { int a; long b; } Union (u) {int a; long b; } Print sizeof(s)and sizeof(u) if sizeof(int)=4 and sizeof(long)=4

2 Answers   Mascot,


Can we assign string to char pointer?

0 Answers  


Are comments included during the compilation stage and placed in the EXE file as well?

0 Answers  






Write a c code segment using a for loop that calculates and prints the sum of the even integers from 2 to 30, inclusive?

4 Answers  


what is the little endian and big endian?

1 Answers  


In the following code segment what will be the result of the function, value of x , value of y { unsigned int x=-1; int y; y = ~0; if(x == y) printf("same"); else printf("not same"); } a) same, MAXINT, -1 b) not same, MAXINT, -MAXINT c) same , MAXUNIT, -1 d) same, MAXUNIT, MAXUNIT e) not same, MAXINT, MAXUNIT

1 Answers   IBM,


The % symbol has a special use in a printf statement. How would you place this character as part of the output on the screen?

0 Answers  


Differentiate b/w Modify and Update commands giving example.

1 Answers  


program to find middle element of linklist?

1 Answers   Huawei,


Look at the Code: #include<string.h> void main() { char s1[]="abcd"; char s2[10]; char s3[]="efgh"; int i; clrscr(); i=strcmp(strcat(s3,ctrcpy(s2,s1))strcat(s3,"abcd")); printf("%d",i); } What will be the output? A)No output B) A Non Integer C)0 D) Garbage

7 Answers   Accenture,


Categories