Tuesday 11 December 2012

Inplement strtok

/*It will tokenize the source string "src" from the place where any of the charcter of the
* target string "tar" will be found. It returns the stating address the token string and
* it should catch by a char pointer
* */

char *mystrtok(char *str, const char *tar)
{
static int i; /*For looping*/
static int len1; /*Holds the length of source string*/
static int len2; /*Holds the length of delimiter string*/
static char *s; /*This variable will holds the address of source string*/
int j ; /*For looping*/
int ptr = 0 ; /*Using for recognizing consecutive delimeter found*/
char *token; /*Variable for storing the token*/
token=(char *)malloc(25*sizeof(char)); /*Creating memory for token*/
if(token == NULL) {
printf("Out of memory space!");
exit(0);
}
/* Defining constraints for first time only*/
if(str != '\0') {
i = 0;
s = str;
len1 = strlen(str);
len2 = strlen(tar);
}
i++;
token=(s + i - 1);
/*tokenizing untill last*/
while(i <= len1+1){
ptr++;
for(j = 0 ; j <= len2 ; j++){
if(*(s + i - 1) == *(tar + j)){
*(token + ptr - 1) = '\0';
if(*token == '\0'){
token++;
ptr = 0;
break;
}
else
return token;
}
if(ptr == 0)
continue;
}
i++;
}
return '\0';
}

No comments:

Post a Comment