473,569 Members | 2,698 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

long word in string

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 DSFSDFSDFDSFASG DFD";

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

Thanks,

Howard

Nov 17 '05 #1
3 4640
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(s tring 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(s ource.Substring (start, currectPostion - start));
}
start = currectPostion + 1;
}
currectPostion ++;
}

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

return longWords;
}
--
With best regards,
Andrew

http://www.codeproject.com/script/pr...asp?id=1181072
"Howard" <ho*******@yaho o.com> wrote in message
news:11******** *************@z 14g2000cwz.goog legroups.com...
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 DSFSDFSDFDSFASG DFD";

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

Thanks,

Howard

Nov 17 '05 #2
Howard wrote:
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(myS tring, "[^ ]{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
--
Expert programming and consulting services available
See http://www.sturmnet.org (try /blog as well)

Nov 17 '05 #3
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,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Howard" <ho*******@yaho o.com> wrote in message
news:11******** *************@z 14g2000cwz.goog legroups.com...
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 DSFSDFSDFDSFASG DFD";

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

Thanks,

Howard

Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
10821
by: Juicer_X | last post by:
Hello everyone, I've been working with the STL Containers for a little while now, but in the middle of working on a small "Markov Chain" class I realized that I wanted to modify my frequency tables, so far I have been using maps like: std::map<std::string, int> frequency; A string for the letter combinations and an int for the...
2
3767
by: Bryan Olson | last post by:
The current Python standard library provides two cryptographic hash functions: MD5 and SHA-1 . The authors of MD5 originally stated: It is conjectured that it is computationally infeasible to produce two messages having the same message digest. That conjecture is false, as demonstrated by Wang, Feng, Lai and Yu in 2004 . Just recently,...
7
2142
by: bryan | last post by:
I think I'm missing something fundamental here. I'm trying to set an unsigned long value via a u_long pointer, however it coredumps everytime I get to that instruction. Here is a sample program that demonstrates the issue: --- snip --- #include <unistd.h> int main() { char buf;
6
2675
by: songie D | last post by:
If a size_t is cast to a long, and size_t is the length of a unicode string, does the resulting long need to be divided by sizeof(_TCHAR) in order to get the actual length in _TCHARs?
17
21306
by: kevgibbo | last post by:
Hi, I'm currently having a problem where a long URL or a line of text with no spaces will break the design of a webpage, http:// blog.seoptimise.com/2007/01/how-to-add-delicious-and-digg-blog- rss.html is an example of this. Does anyone know if it's possible to break this text into multiple lines using CSS?
2
1926
by: theronnightstar | last post by:
I am writing an anagram program for my fiance. Figured it would be an excellent task to learn from. The way it is supposed to work is it reads in a word list from a file into a temporary vector<string>. From that it selects a random word 6 letters or more long. That is the word of the game - the one to make all the anagrams from. After it...
73
7373
by: Yevgen Muntyan | last post by:
Hey, I was reading C99 Rationale, and it has the following two QUIET CHANGE paragraphs: 6.5.3.4: "With the introduction of the long long and extended integer types, the sizeof operator may yield a value that exceeds the range of an unsigned long." 6.5.6: "With the introduction of the long long and extended integer
0
6178
by: altavim | last post by:
Usually when you make regular expression to extract text you are starting from simple expression. When you got to know target text, you are extending your expression. Subsequently very hard to ready long set of special symbols and impossible to improve such expression. We have to create ’smart’ regular expression. Instead of write one line...
6
4261
by: jephperro | last post by:
Hi there, I'm having a really tough time with a SQL statement and I am wondering if someone is able to help out or point me in the right direction. I have a table of names which can be very long. These names get printed on envelopes. The problem is the envelope can only hold 35 characters per line. I have to divide into 4 lines at...
5
1921
by: josh001 | last post by:
This Error message pops up every time I run my program using Python Shell and won't open at all when I don't use Shell. Traceback (most recent call last): File "C:\Documents and Settings\Joshua Leihe\My Documents\My Projects\NewProjects\Joshua\Python projects\French quiz\French Quiz_17.pyw.pyw", line 440, in <module> win.start() ...
0
7697
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8120
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7672
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6283
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5512
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.