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 />");
}
}

Change template parameters,

...
     SortedList<string, int> s1 = new SortedList<string, int>();
          
     foreach (string k in Words)
      {
       if (s1.ContainsKey(k))
          s1[k]++;
       else
           s1.Add(k, 1);
      }
      foreach (KeyValuePair<string, int> kvp in s1)
         Response.Write(kvp.Key + " : " + kvp.Value);
 ...
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.