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
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
The logic what you have written is wrong use the following:
for(int i=0;i<strText.Length;i++)
{
if(strText[i] == 'g')
LetterCount++;
}
- Proposed As Answer by Doemae Saturday, August 11, 2012 8:20 AM
- Marked As Answer by Jason Dot WangMicrosoft Contingent Staff, Moderator Monday, August 20, 2012 8:13 AM
-
Saturday, August 11, 2012 8:03 AM
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";
}- Proposed As Answer by amit_kumar Saturday, August 11, 2012 8:05 AM
- Marked As Answer by Jason Dot WangMicrosoft Contingent Staff, Moderator Monday, August 20, 2012 8:13 AM
-
Saturday, August 11, 2012 8:24 AM
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";- Proposed As Answer by ProgrammingVolunteerMVP Saturday, August 11, 2012 8:31 AM
- Marked As Answer by Jason Dot WangMicrosoft Contingent Staff, Moderator Monday, August 20, 2012 8:13 AM
-
Saturday, August 11, 2012 8:28 AM
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)
我的博客园
慈善点击,点击此处- Edited by ProgrammingVolunteerMVP Saturday, August 11, 2012 8:29 AM Modify codes
- Marked As Answer by Jason Dot WangMicrosoft Contingent Staff, Moderator Monday, August 20, 2012 8:14 AM
-
Saturday, August 11, 2012 6:39 PM
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
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?

