Find # of Occurences & count in string using Collections/Generics

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DexterID
    New Member
    • Mar 2010
    • 3

    Find # of Occurences & count in string using Collections/Generics

    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:
    Code:
    <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:
    Code:
    //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 />");
    }
    }
  • PRR
    Recognized Expert Contributor
    • Dec 2007
    • 750

    #2
    Try this:
    Code:
    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
    Code:
    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);
                }
    
            }

    Comment

    • DexterID
      New Member
      • Mar 2010
      • 3

      #3
      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.

      Comment

      Working...