S
shyam
Hi All
Here is a program which basically tokenizes a string based on space
seperation.
But it does not run bcoz i am directly trying to manuplate the string.
I cannot use standard string functions due to performance issues
can anybody figure out whats gone wrong with this
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#define SP ' '
char **tokenize(char *);
char ** tokenize(char *ch)// to tokenize the string based on space
{
int count = 0;
char *mrk = ch;
while(*ch != '\0')
{
if(*ch == SP)
count++;
ch++;
}
count = count + 2;
ch = mrk;
char **c = (char **)malloc(count * sizeof(ch));
int count2;
char *fst,*lst;
fst = lst = ch;
for (count2 = 0; count2 < count - 1; count2++)
{
lst = strchr(lst,SP);
*lst = '\0'; //the DDD crashes at this point
lst++;
c[count2] = fst;
fst = lst;
}
c[count - 1] = NULL;
return c;
}
int main()
{
char *s = "hello how do u do";
char **t = tokenize(s);
printf("the str is %s\n",*t[0]);//similarly for s[1] & s[2]
}
Here is a program which basically tokenizes a string based on space
seperation.
But it does not run bcoz i am directly trying to manuplate the string.
I cannot use standard string functions due to performance issues
can anybody figure out whats gone wrong with this
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#define SP ' '
char **tokenize(char *);
char ** tokenize(char *ch)// to tokenize the string based on space
{
int count = 0;
char *mrk = ch;
while(*ch != '\0')
{
if(*ch == SP)
count++;
ch++;
}
count = count + 2;
ch = mrk;
char **c = (char **)malloc(count * sizeof(ch));
int count2;
char *fst,*lst;
fst = lst = ch;
for (count2 = 0; count2 < count - 1; count2++)
{
lst = strchr(lst,SP);
*lst = '\0'; //the DDD crashes at this point
lst++;
c[count2] = fst;
fst = lst;
}
c[count - 1] = NULL;
return c;
}
int main()
{
char *s = "hello how do u do";
char **t = tokenize(s);
printf("the str is %s\n",*t[0]);//similarly for s[1] & s[2]
}