Reputation: 23
I'm trying to write a program where the user gives the system a word, and a paragraph, the system's job is to count how many times that word pops up.
How can I count how many times the word pops up in C#?
Upvotes: 1
Views: 2294
Reputation: 4606
Using regular expression with Word Boundary anchor:
int wordCount = Regex.Matches(text, "\\b" + Regex.Escape(searchTerm) + "\\b", RegexOptions.IgnoreCase).Count;
Upvotes: 4
Reputation: 7320
String test = "the full full :full? text !!! ";
String search = "full";
int count = String.Concat(test.Select(i => Char.IsPunctuation(i) ? ' ' : i))
.Split(' ').Where(i => i == search).Count();
This will:
test.select
), and will put them together again as another string (String.Concat
).Split
)search
stringCount()
Upvotes: 0
Reputation: 3469
https://msdn.microsoft.com/en-us/library/bb546166.aspx
as the article says " There is a performance cost to the Split method. If the only operation on the string is to count the words, you should consider using the Matches or IndexOf methods instead
"
So you could use a while loop with indexOf and count if performance is an issue.
class CountWords
{
static void Main()
{
string text = @"Historically, the world of data and the world of objects" +
@" have not been well integrated. Programmers work in C# or Visual Basic" +
@" and also in SQL or XQuery. On the one side are concepts such as classes," +
@" objects, fields, inheritance, and .NET Framework APIs. On the other side" +
@" are tables, columns, rows, nodes, and separate languages for dealing with" +
@" them. Data types often require translation between the two worlds; there are" +
@" different standard functions. Because the object world has no notion of query, a" +
@" query can only be represented as a string without compile-time type checking or" +
@" IntelliSense support in the IDE. Transferring data from SQL tables or XML trees to" +
@" objects in memory is often tedious and error-prone.";
string searchTerm = "data";
//Convert the string into an array of words
string[] source = text.Split(new char[] { '.', '?', '!', ' ', ';', ':', ',' }, StringSplitOptions.RemoveEmptyEntries);
// Create the query. Use ToLowerInvariant to match "data" and "Data"
var matchQuery = from word in source
where word.ToLowerInvariant() == searchTerm.ToLowerInvariant()
select word;
// Count the matches, which executes the query.
int wordCount = matchQuery.Count();
Console.WriteLine("{0} occurrences(s) of the search term \"{1}\" were found.", wordCount, searchTerm);
// Keep console window open in debug mode
Console.WriteLine("Press any key to exit");
Console.ReadKey();
}
}
/* Output:
3 occurrences(s) of the search term "data" were found.
*/
Upvotes: 1