-1

I am new to this language and still learning, I have a windows form application that has 2 textboxes on it and a one-button, the first box is where the user input a sentence or word, the second box is where the user inputs a single character to find if it's in the sentence, the button is for counting how many multiple occurrence of a character to the sentence.

example:
txtbox1: Hello World!
txtbox2: o
display: 2

what is the easier to do? or recommended?

Seonix
  • 11
  • 3

2 Answers2

2

From this answer, you can use .Count().

var source = txtbox1.Text;
var charToMatch = txtbox2.Text.First();
var count = source.Count(f => f == charToMatch);
MD. Khairul Basar
  • 4,976
  • 14
  • 41
  • 59
0
public int count(string s,string ch)        //s is the string and ch is the char to look for
{
    int count = 0;
    for (int i = 0; i < s.Length; i++)
        if(s[i]==ch[0])
            count++;
    return count;
}

private void button1_Click(object sender, EventArgs e)
{
    if (tbString.Text != "" && tbChar.Text != "")
        tbCount.Text = count(tbString.Text, tbChar.Text).ToString();
}
MD. Khairul Basar
  • 4,976
  • 14
  • 41
  • 59
yoav28
  • 28
  • 6