i need to write a program that recieves a data file with spaces and characters(words)
and i need to determine how many words are in the data file..pleeze help..
Printable View
i need to write a program that recieves a data file with spaces and characters(words)
and i need to determine how many words are in the data file..pleeze help..
Go Step By step through out the file.... In a for next loop... here is
somesuedo code in VB.net but it will translate in to c# fine.
Youll want to load the file into a buffer..
data buffer as string
wordstart as boolean = false
wordcount as integer
for X = 1 to (length of data buffer)
hold = mid(data buffer,X,1)
If hold <> " " then
wordstart = true
else:
if wordstart = true then
wordstart = false
wordcount = wordcount + 1
else:End if
end if
next x
Here's a shorter method.
1. Read the file into a string
2. Split the resulting string using a space as a delimiter. That returns an
array of strings.
3. Count the number of items in the array.
Example
public int getWordCount(String filename) {
FileInfo fi = new FileInfo(filename);
String[] words;
String s;
StreamReader sr = fi.OpenText();
s = sr.ReadToEnd();
sr.Close();
words= s.Split(' ');
return words.Length;
}
Add your own error trapping. You could get much more sophisticated than
this. For example, the code shown overcounts when the file contains more
than one space in a row, and undercounts words that aren't separated with a
space, such as when counting words in code.
Russell Jones
Sr. Web Development Editor,
DevX.com
"Jackee" <ladyeagle_01@yahoo.com> wrote in message
news:3aefb1af@news.devx.com...
>
> i need to write a program that recieves a data file with spaces and
characters(words)
> and i need to determine how many words are in the data file..pleeze help..