Logic Error Help (Newbie trying to learn...)

Answered Logic Error Help (Newbie trying to learn...)

  • Friday, August 10, 2012 11:41 PM
     
     

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    namespace WindowsFormsApplication1
    {
        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(0,1);

                    if (Letter == "g")
                    {
                        LetterCount++;
                    }

                }
                textBox1.Text = "g appears " + LetterCount + " times";
            }
        }
    }

    I am trying to do a tutorial from this website : 

    http://www.homeandlearn.co.uk/csharp/csharp_s5p3.html

    and I am having a hard trying to solve this. I think the solution to this Logic Error located at this line of code:

    Letter = strText.Substring(0,1);

    no, I am sure of it!

    Tips please! Thanks!

All Replies

  • Saturday, August 11, 2012 12:17 AM
     
     Answered Has Code

    You are correct. That line is always getting the first character in the string "Debugging" (starts at location zero and gets one character). You want to get each character in turn, the first, second, third, etc. each time through the loop. The way to do this is by using the loop counter, "i".
                    Letter = strText.Substring(i,1); // Get the ith character.

    • Proposed As Answer by csdevrich Saturday, August 11, 2012 4:07 AM
    • Marked As Answer by TheVincent Saturday, August 11, 2012 6:37 PM
    •  
  • Saturday, August 11, 2012 7:02 AM
     
     Answered

    The logic what you have written is wrong use the following:

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

    {

         if(strText[i] == 'g')

           LetterCount++;

    }

  • Saturday, August 11, 2012 8:03 AM
     
     Answered

    Try this one

    private void button4_Click(object sender, EventArgs e)
            {
                int LetterCount = 0;
                string strText = "Debugging";
                string Letter;

                for (int i = 0; i < strText.Length; i++)
                {
                    Letter = strText[i].ToString();

                    if (Letter == "g")
                    {
                        LetterCount++;
                    }

                }
                textBox1.Text = "g appears " + LetterCount + " times";
            }

  • Saturday, August 11, 2012 8:24 AM
     
     Answered Has Code

    or in this way

                int LetterCount = 0;
                string strText = "Debugging";
    
                foreach(char obj in strText)
                    if (obj == 'g') LetterCount++;
    
                textBox1.Text = "g appears " + LetterCount + " times";

  • Saturday, August 11, 2012 8:28 AM
     
     Answered

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    namespace WindowsFormsApplication1
    {
        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(0,1);

                    if (Letter == "g")
                    {
                        LetterCount++;
                    }

                }
                textBox1.Text = "g appears " + LetterCount + " times";
            }
        }
    }

    I am trying to do a tutorial from this website : 

    http://www.homeandlearn.co.uk/csharp/csharp_s5p3.html

    and I am having a hard trying to solve this. I think the solution to this Logic Error located at this line of code:

    Letter = strText.Substring(0,1);

    no, I am sure of it!

    Tips please! Thanks!


    Correct another way——if u are using 3.5 version of net framework or above……

     textBox1.Text = strText.Text.Where(c=>c=='9').Count().ToString();


    下载MSDN桌面工具(Vista,Win7)
    我的博客园
    慈善点击,点击此处


  • Saturday, August 11, 2012 6:39 PM
     
      Has Code

    Wow, that was so simple yet can be confusing for a newbie like me. Thanks for clearing it up. I have tried:

     Letter = strText.Substring(i,1+i); 

    but that would only add more characters into the counter. LOL


    • Edited by TheVincent Saturday, August 11, 2012 6:40 PM spelling
    •  
  • Sunday, August 12, 2012 12:58 AM
     
      Has Code

    Wow, that was so simple yet can be confusing for a newbie like me. Thanks for clearing it up. I have tried:

     Letter = strText.Substring(i,1+i); 

    but that would only add more characters into the counter. LOL


    Hi:D

    Have u tried my way?


    下载MSDN桌面工具(Vista,Win7)
    我的博客园
    慈善点击,点击此处