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(Occurrenc es) 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 3546 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 NEW so I don't have to retrieve them all. If
you have experience with this kind of thing, you know that the server creates
unique IDs for all the messages, but this IDs are not guaranteed to be
unique, since they can be reused once a message is...
|
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>
<id>Supplier123</id>
</customer>
|
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 text,String Character)
{
int count = 0;
|
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 for the objects on the
form, in addition the The objects are created in the fpositional class and
affixed to the Input form through the fpositional constructor which takes the
form as an argument. The ComboBox and TextBox Array classes hold the...
|
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 find the key.
In the example below, accessing the dictionary by using the exact key object
that was used to add to the dictionary works.
(see code comment 1).
However, if I attempt to access the dictionary by using a key object that
| |
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 binding it to a repeater
control. This repeater control has multiple text boxes and buttons. Can
you please tell me how can i do paging in this case ?
I'm posting my code below. The problem is that if i click on
"AdjustThisAd" button, it opens...
|
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 using a GridView that has ViewState
enabled, an ObjectDataSource to process the submitted list, textboxes for
getting new item data and an add button.
Does this approach sound feasible or are there better alternatives?
My problem is that I don't...
|
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 (MyBase) and a collection of MyBase objects
(MyBaseCollection). Now, I also have an extension to MyBase (MyExtendedBase
: MyBase) that has additional properties. And lastly, I have a collection
of MyExtendedBase objects(MyExtendedBaseCollection :...
|
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 files could be upwards of 25mb in size
and time is important. I don't want to take the route of loading the text of
the file into a giant string and searching it, but would rather focus on a
performance-minded solution. Any sugesstions for a...
|
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 given file.
I tried my level best even after the quiz to come up with a solution
but cudnt find an efficient one. :(
This is what I did.
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |