Hi,
I tried so much working with this to get distinct Words and their count. I'm using ASP.NET 2.0.
And i also googled so much. I need HELP from anyone to get the required output.
1)Ex(String): The DOTNET is Very Cool! The Best.
2) Capture all the sequences like Spaces, Fullstops, Question Marks, Exclamations, Apostrophes, New Lines....(May be with RegEx?)
3)Split Words According to the above sequences
4)Get Distinct Words(Occurrences) and their count based on the input string in 1st statement.
Desired O/P:
The - 2
DOTNET - 1
Is - 1
Very - 1
Cool - 1
Best - 1
My Sample Code is as follows
ASPX: -
<asp:TextBox ID="txtString" runat="server" TextMode="MultiLine" Height="100px" Width="500px"></asp:TextBox>
-
<asp:Button ID="btnSubmit" runat="server" Text="Submit" onclick="btnSubmit_Click" />
-
CS: -
//Is it possible with Dictionary
-
SortedList<int, string> sl = new SortedList<int, string>();
-
-
protected void btnSubmit_Click(object sender, EventArgs e)
-
{
-
//Here i'ld like to check for Space, Question Mark, New Line, Exclamation, Apostrophe,.....
-
//Now i'm checking only for spaces. I need the above sequences loop also.
-
string[] Words = txtString.Text.Split(' ');//Space Split
-
for (int i = 0; i < Words.Length; i++)
-
{
-
sl.Add(i, Words[i]);
-
}
-
-
foreach (KeyValuePair<int, string> kvp in sl)
-
{
-
//I want to print Distinct Words and their Count
-
Response.Write(kvp.Value + " " + kvp.Key + "<br />");
-
}
-
}
-
2 3533 PRR 750
Recognized Expert Contributor
Try this: -
-
public static void WordsinString(string str)
-
{
-
char[] sep = new char[] { ' ' };
-
Dictionary<string,int> strCount=new Dictionary<string,int>();
-
-
string[] temp = str.Split(sep,StringSplitOptions.RemoveEmptyEntries);//, StringSplitOptions.RemoveEmptyEntries);
-
-
-
foreach (string s in temp)
-
{
-
//Console.WriteLine(s);
-
-
if (strCount.ContainsKey(s))
-
{
-
strCount[s] = (int)strCount[s] + 1;
-
}
-
-
else
-
{
-
strCount.Add(s,1);
-
}
-
-
-
}
-
-
foreach (KeyValuePair<string, int> p in strCount)
-
{
-
Console.WriteLine(p.Key+" "+ p.Value);
-
}
-
-
-
}
-
or -
public static void WordsInString(string str)
-
{
-
string[] temp = str.Split(new char[] { ' '}, StringSplitOptions.RemoveEmptyEntries);
-
-
Dictionary<string, int> strCount = new Dictionary<string, int>();
-
-
Regex rgx; //= new Regex(pat, RegexOptions.IgnoreCase);
-
MatchCollection matches ;//= rgx.Matches(str);
-
-
foreach (string s in temp)
-
{
-
rgx=new Regex(s, RegexOptions.IgnoreCase);
-
-
matches = rgx.Matches(str);
-
-
if (!strCount.ContainsKey(s))
-
{
-
strCount.Add(s, matches.Count);
-
}
-
-
-
}
-
-
foreach (KeyValuePair<string, int> p in strCount)
-
{
-
Console.WriteLine(p.Key + " " + p.Value);
-
}
-
-
}
-
char[] sep = new char[] { ' ', '.', '?', '!', '\n', '\r',',' };
worked perfectly. Thanks. I/P:
the the.the?the!the
the, O/P:
the 6
Thanks a lot PRR.
Thanks for ur Dexterity.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Madestro |
last post by:
Hi guys,
I am making a small program to retrieve e-mails from POP accounts. I got all
the e-mail parsing stuff figured out, but I cannot seem to come up with a way
to find out which e-mails are...
|
by: Niklas E |
last post by:
Does anyone know how to get all nodes in a xml-document that start with a
certain text?
In my case I want to count all suppliers ( <id>Supplier*</id> )
<root>
<customer>
<name>ABC</name>...
|
by: Jason Gleason |
last post by:
What's the most efficient way to get the number of occurences of a certain
string in another string..for instance i'm using the following code right
now...
private int CharacterCounter(String...
|
by: Poewood |
last post by:
Okay here are four classes for a pocket pc program: Input, fpositional,
ComboBoxArray and TextBoxArray. The "input" class is the form. I use the
fpositional class to handle most of the functions...
|
by: ESPNSTI |
last post by:
Hi,
I'm trying to use a generics dictionary with a key class that implements and
needs IComparable<>.
However when I attempt to use the dictionary, it doesn't appear to use the
IComparable<> to...
| |
by: ratnakarp |
last post by:
Hi,
I have a search text box. The user enters the value in the text box and
click on enter button. In code behind on button click i'm writing the
code to get the values from the database and...
|
by: John_H |
last post by:
Re: ASP.NET 2.0
I would like suggestions or code examples on how to collect a variable
length list of input data (item# & item quantity specifically).
I thought that I could accomplish this...
|
by: Jerad Rose |
last post by:
I'm relatively new to C# and polymorphism, so what I'm trying to accomplish
may not be possible, or there may be a totally different approach that I
should be taking.
I have a base class...
|
by: Dameon |
last post by:
Hi All,
I have a process where I'd like to search the contents of a file(in a dir)
for all occurences (or the count of) of a given string. My goal is to focus
more on performance, as some of the...
|
by: Neehar |
last post by:
Hello
For one of the interviews I took recently, I was given an offline
programming quiz.
In 30 minutes I had to write code in C++ to counts the number of times
each unique word appears in a...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |