What happened to my C # For Loop and If statement?

int LetterCount = 0;
string strText = "Debugging";
string letter;

for (int i = 0; i <strText.Length; i++)
{
  letter = strText.Substring(0, 9);
  if(letter == "g")
  {    
    LetterCount++;
    textBox1.Text = "g appears " + LetterCount + " times";
  }
}

So, I am doing this training thing, and I am doing this exercise like 4 hours. And I can’t understand what’s wrong with my For Loop.

The point of the exercise is to get my program to tell me how much g is in the debugging word. But you probably understood that. Anyway, I'm not even sure that I have the correct code to tell me this, because I think I need to change the second part of the For Loop (i <) part.

, "if letter ==" g "" . , , , = , , g 24 , ( str.length 9 ?) 0 , .

+3
10

9 . "g" ( ). .

int count = 0;
foreach (char c in strText)
{
    if (c == 'g')
       count++;
}

for:

for (int i = 0; i < strText.Length; i++)
{
    if (strText[i] == 'g')
       count++;
}
+7

string.Substring(x, y).

:

letter = strText.Substring(0, 9);

. 9 strText. , i , .

( , , , , , , , , , =)

+5

:

    for (int i = 0; i <strText.Length; i++)
    {

       if(strText[i] == 'g')
       {
         LetterCount++;
       }
    }
    textBox1.Text = "g appears " + LetterCount + " times";

, , "g". , , . , , , , .

+1

i for.

letter = strText.Substring(i, 1);

?

0

, , 9 charachters g. .

:

letter = strText.Substring(i,1);
0

String.Substring(int, int) : , .

letter = strText.Substring(0, 9); "". , letter = strText.Substring(i, 1).

0

, - :

int LetterCount = 0;
string strText = "Debugging";
string letter;

for (int i = 0; i <strText.Length; i++)
{
  letter = strText.Substring(i, 1);
  if(letter == "g")
  {    
    LetterCount++;
    textBox1.Text = "g appears " + LetterCount + " times";

  }
}
0

letter = strText.Substring(0, 9);

"letter" "", .

letter = strText[i], .

0

@Rob.

- :

int    gCount = 0;
string s      = "Debugging";

for ( int i = 0; i <strText.Length; i++)
{
  if ( s[i] == 'g' ) ++gCount ;
}
textBox1.Text = "g appears " + gCount+ " times";
0
namespace runtime
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            int lettercount = 0;
            string strText = "Debugging";
            string letter;


            for (int i = 0; i < strText.Length; i++)
            {
                letter = strText.Substring(i,1);

                if (letter == "g")
                {
                    lettercount++;
                }

            }
            textBox1.Text = "g appear " + lettercount + " times";
        }
    }
}
0

All Articles