Tag. Ich hab mir folgende Funktion zum Wörterzählen (u.a.) geschrieben. Jetzt meine Frage: Kann man da noch was optimieren?
Code:
public static WordCounter CountWords(string text2Count)
{
WordCounter Counter = new WordCounter();
string str;
int i = 0, j = text2Count.Length;
bool inWord = false;
while(i<j)
{
switch(str = text2Count[i].ToString())
{
case "\r":Counter.paragraphs++;inWord = false;break;
case "\n":inWord = false;break;
case "\t": Counter.tabs++;inWord = false;break;
case " ": Counter.whites++; inWord = false; break;
case "!":
case "\"":
case "?":
case ",":
case ";":
case ".":
case ":":
case "-":
case "'":
case "(":
case ")": Counter.marks++; break;
default :
if(!inWord)
{
Counter.words++;
inWord = true;
}
break;
}
i++;
}
return Counter;
}
WordCounter ist wie folgt definiert:
Code:
public struct WordCounter
{
public int words; //words in text
public int marks; //punctuation marks
public int whites; //whitespaces
public int paragraphs; // ...
public int tabs; //tabstops
}
Greetz Sergej