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

3
  • BTW, you should also consider using string.Equals when comparing two strings, because you can set whether comparison should be case and linguistically sensitive. Commented Jun 13, 2011 at 22:01
  • You don't need to update that text box every time you run through the loop. Move this line after your for loop. textBox1.Text = "g appears " + LetterCount + " times"; Commented Jun 13, 2011 at 22:02
  • Omg. So, the tutorial was stupid. It told me to put 0, 1. Commented Jun 13, 2011 at 22:05

10 Answers 10

7

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++;
}
Sign up to request clarification or add additional context in comments.

8 Comments

@Jonathan, the downside to this is it's a foreach not a for loop, so the OP won't gain an understanding of a "plain vanilla" for loop from it, which I'd guess is the main intent of the tutorial they're following; or at least the current section =)
I would probably do the loop in just one line just to impress the teacher: foreach (char c in strText) if (c == 'g') count++; :o)
@Rob: Well, I did point out what seems to be the primary error in my mind. As indicated, that's just probably the approach I'd take. But, yes, if this is some sort of school assignment, hey may not yet be familiar with foreach.
@balexandre: Heh, well in that case, perhaps you'd like a compact LINQ statement.
added code if he needs to use the for loop instead the foreach
|
5

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 =)

2 Comments

Well I had 0,1, but that was just getting me stuck at D. I think the tutorial I was reading was wrong, because it told me to put 0, 1. But putting i, 1 fixed it. Thank you :D <3
@Cole, glad I could help =) It sounds very much like the tutorial you're working through had a typo (or three)! Oh and welcome to stackoverflow =)
1

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.

3 Comments

I think you meant letter[i] == 'g'.
Not sure that your first line is correct, more or less, you've just copied the value of strText. Did you mean to use .ToCharArray() instead?
@ben, no I didn't plan on using ToCharArray. Which part is it that you think is not correct? The for loop?
0

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

Do you mean

letter = strText.Substring(i, 1);

?

2 Comments

I'm fairly blind. I couldn't tell if it was an I or a 1 on the tutorial :[
Happens more and more as you get older. :)
0

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

Comments

0

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).

Comments

0

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

  }
}

Comments

0

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.

Comments

0

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.