DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 3 of 3

Thread: word count!!!

  1. #1
    Jackee Guest

    word count!!!


    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..

  2. #2
    Jay King Guest

    Re: word count!!!

    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



  3. #3
    Russell Jones Guest

    Re: word count!!!

    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..




Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links