I am doing a home work question to split a string without using the framework method.
Following is the working code I came up with.
I would like to know how can I improve the running time to O(n)?
Also any suggestions on improvement are welcome.
public static string[] split(string txt, char[] delim)
{
char[] text = txt.ToCharArray();
string[] result = new string[0];
int count = 0;
int i = 0;
StringBuilder buff = new StringBuilder();
while(i < text.Length)
{
bool found = false;
foreach(char del in delim)
{
if(del == txt[i])
{
found = true;
break;
}
}
if(found)
{
count++;
Array.Resize(ref result, count);
result[count - 1] = buff.ToString();
buff = new StringBuilder();
}
else
{
buff.Append(txt[i]);
}
i++;
}
if(buff.Length != 0)
{
count++;
Array.Resize(ref result, count);
result[count - 1] = buff.ToString();
}
return(result);
}