long word in string

H

Howard

Can anyone think of a better algorithm for this function? Since longStr
could be very large in size (~100k).

The function will return a string array with words that are larger than
n in length. Where n is a positive integer.

string longStr = "asdfd DSFSDFSDFDSFASGDFD";

void FindLongWord(int length)
{
string [] array;
array = longStr.split(' ');
for(int i =0;i < array.Length; i++)
{
if (array.Length >= length)
{
//add to arraylist...
}
}
}



Thanks,

Howard
 
A

Andrew Kirillov

Hello

You can just scan your source string and collect long words. So, you will
need much more less memory, only for your long words.

private ArrayList FindLongWords(string source, int len)
{
int start = 0;
int currectPostion = 0;
int length = source.Length;
ArrayList longWords = new ArrayList();

// scan the source string
while (currectPostion < length)
{
// find next space
if (source[currectPostion] == ' ')
{
if (currectPostion - start >= len)
{
longWords.Add(source.Substring(start, currectPostion - start));
}
start = currectPostion + 1;
}
currectPostion ++;
}

if (currectPostion - start >= len)
{
longWords.Add(source.Substring(start, currectPostion - start));
}

return longWords;
}
 
O

Oliver Sturm

Howard said:
Can anyone think of a better algorithm for this function? Since longStr
could be very large in size (~100k).

The function will return a string array with words that are larger than
n in length. Where n is a positive integer.

I don't know about efficiency, but you could easily use a regular
expression for this:

Regex.Match(myString, "[^ ]{10,}")

This would return a collection of matches representing words (character
sequences that don't contain space) of ten characters length, or longer.

You might want to do some tests to find out how this approach compares to
your sequential algorithm.


Oliver Sturm
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

This seems like a homework :)
in any case is very easy, just iterate the string, keep a counter that you
reset when you find a space, when you find a space see if the diff between
the current index and the last mark is big enough if so add the substring to
the arraylist.

at the end convert the arraylist to array and you are ready

cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top