c# - Count the number of words within an Array or List -
i need count number of words within array or list. reason array or list because not sure best use in situation. data static , in .txt file (it's book). able create array , break down words array life of me can not count! have tried many different ways , i'm thinking since string unable count. have teetered on edge of printing whole book listbox , counting listbox but, that's ridiculous.
public partial class mainform : form { //------------------------ //global variables: //------------------------ list<string> countwords; string[] filewords; string[] filelines; char[] delim = new char[] { ' ', ',','.','?','!' }; string path; public mainform() { initializecomponent(); } private void booktitle() // titleandauthor method pull book title , display it. { (int = 0; < 1; i++) { booktitlelabel.text = filelines[i]; } } private void bookauthor() // titleandauthor method pull book author , display it. { (int = 1; < 2; i++) { bookauthorlabel.text = filelines[i]; } } private void firstlines() // firsttenwords method pulls first ten words of text file , prints listbox { (int = 0; <= 499; i++) { wordslistbox.items.add(filewords[i]); } } private void wordcount() // count words in file. { } private void openfilebutton_click(object sender, eventargs e) { openfiledialog inputfile = new openfiledialog(); if (inputfile.showdialog() == dialogresult.ok) // check file user selected { path = inputfile.filename; // save path of file string variable later use streamreader fileread = new streamreader(path); // read file @ path outlined in path variable filewords = fileread.readtoend().split(delim); // breakdown text lines of text call them @ later date filelines = file.readalllines(path); countwords = file.readlines(path).tolist(); wordslistbox.items.clear(); booktitle(); bookauthor(); firstlines(); wordcount(); } else { messagebox.show("not valid file, please select text file"); } } }
maybe useful:
static void main(string[] args) { string[] lines = file_readalllines(); list<string> words = new list<string>(); foreach(var line in lines) { words.addrange(line.split(' ')); } console.writeline(words.count); } private static string[] file_readalllines() { return new[] { "the 1 book", "written gnarf", "once upon time ther werent grammer", "iso 1-12122-445", "(c) 2012 under hills" }; }
Comments
Post a Comment