Problem statement:
Wanted to have an optimized split function which will search for given character in string and split the string into 2 sub-strings, before and after the character.
Example :
s1 = strdup("XYZ@30");
s2 = split(s1,"@");
Should get following output.
s1 = "XYZ"
s2 = "30"
I have written following split(), but could somebody help me optimize it.
char * split(char *str1, char *ch)
{
int i=0;
char *str2;
if(!(str1 && ch))
return NULL;
else
{
str2 = strdup(str1);
while('\0'==str2)
{
if(*str2==*ch)
{
i++;
str1[i]='\0';
return (++str2);//If the ch is last in str1, str2 can be NULL
}
str2++;
}
}
return NULL;
}