Cole
Cole

Reputation: 33

What's wrong with 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'm doing this tutorial thing, and I've been stuck on this exercise for like 4 hours. And I can't figure out what's wrong with my For Loop.

The point of the exercise is to make my program thing tell me how many g's are in the word debugging. But you probably figured that out. Anyway, I'm not even sure that I have the right code for telling me that, because I think that I need to change the second part of the For Loop (the i < ) part.

But my problem is that it isn't registering the "if letter == "g" " at all. Because according to my locals window it says that letter=Debugging, which would make me think that g should be registering on my program 24 times, I think (because str.length is 9 letters long?) But it's registering as 0 no matter what I do.

Upvotes: 3

Views: 622

Answers (10)

deepak
deepak

Reputation: 1

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

Upvotes: 0

Jonathan Wood
Jonathan Wood

Reputation: 67213

You are extracting a string of 9 characters. It will never be equal to "g" (which only has one). Here's how I'd do it.

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

Using the for loop:

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

Upvotes: 7

Nicholas Carey
Nicholas Carey

Reputation: 74277

What @Rob said.

Try something like this:

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";

Upvotes: 0

Babak Naffas
Babak Naffas

Reputation: 12561

letter = strText.Substring(0, 9);

at this point, 'letter' has the value "Debugging" since you're taking the entire string.

Try letter = strText[i] so you isolate the single letter.

Upvotes: 0

brendan
brendan

Reputation: 29986

You're probably looking for something like this:

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";

  }
}

Upvotes: 0

Roman Royter
Roman Royter

Reputation: 1665

Because String.Substring(int, int) takes two arguments: the offset and amount to take.

In your case, letter = strText.Substring(0, 9); will simply assign letter's value to "Debugging". If you want to check each letter individually, you need to write letter = strText.Substring(i, 1).

Upvotes: 0

Ivan I
Ivan I

Reputation: 9990

Well, you are taking substring that is long 9 charachters and comparing it to "g". It won't be equal.

You should try:

letter = strText.Substring(i,1);

Upvotes: 0

Abe Miessler
Abe Miessler

Reputation: 85056

Try this:

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

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

The issue is that you are looking at the entire string when you compare to "g". By specifying an index you are telling it to look at a specific character in the string. Also, I removed your substring because it did not appear to be doing anything.

Upvotes: 1

John
John

Reputation: 16007

You're not using i at all in your for loop.

Do you mean

letter = strText.Substring(i, 1);

?

Upvotes: 0

Rob
Rob

Reputation: 45771

Take a look at the documentation for string.Substring(x, y).

Basically:

letter = strText.Substring(0, 9);

Isn't giving you a letter. Each time through it's giving you all 9 characters of the string strText. You might want to consider using the variable i for one of the values you pass to Substring.

(I've deliberately not given you the entire answer as you seem to want to understand, so, if the pointers I've given don't get you there, let me know and I'll expand my answer =)

Upvotes: 5

Related Questions