Sunday, October 25, 2015

Character and word count in c#

 private void character(string p)
    {
        string s = p;
        int count = 0;
        try
        {
        char c=Convert.ToChar(TextBox2.Text);
       
            foreach (char item in s)
            {
                if (item == c)
                {
                    count++;
                }
            }
            Response.Write("no of " + c + " are " + count);
        }
        catch (Exception )
        {
            Response.Write("invalid format given");
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        wordcount(TextBox1.Text);
    }

    private void wordcount(string p)
    {
        int count = 0;
        string s = p;
        string c = TextBox2.Text;
        foreach (Match item in Regex.Matches(s,c,RegexOptions.IgnoreCase))
        {
            count++;
        }
     
            Response.Write("word" + c + "count is " + count);
        }
        

No comments:

Post a Comment