#1 Members - Reputation: 143
Posted 24 August 2012 - 08:03 AM
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";
}
Also I would like to ask, how long did it take you guys to get the hang of programming? I'm having to practically start over with all this. I barely passed my first year at university, I missed half of the year as I was in hospital and I've never been able to fully catch up with programming. I really want to be able to get the hang of it but at the moment it just seems like its never going to happen.
#4 Members - Reputation: 120
Posted 24 August 2012 - 09:00 AM
Instead of setting the first parameter to zero, set it to i so that it iterates through the string.
#6 Members - Reputation: 269
Posted 24 August 2012 - 01:33 PM
Oh right now I see instead of it constantly being set to zero if I set it to i it puts it through the loop?
The loop is already present. It's your 'for' command:
[source lang="csharp"]for (int i =0; i < strText.Length; i++){ //Everything inside of these brackets is performed until i = (strText.Length - 1)}[/source]
And because the Substring(x,y) command is inside of the curvy brackets, it is performed with each loop iteration. So it is already being 'put through the loop' (as you questioned). The function Substring(x,y) has two parameters. The first (labeled 'x' here) is the starting index and the second (labeled 'y') is the number of characters to read from 'x'. What's important to understand is that the computer doesn't have the ability to read like a human it keeps the string contents as an array of characters - . The word "Debugging" would look something like: {'D','e','b','u','g','g','i','n','g'}. And by using the substring command, you're asking the computer to return a new array of characters 'y' characters long starting from position 'x'. In your case, you were asking for a new 'string' of characters 1 character long starting from position 0.
By following @soylentrob's advice, you're now asking for a new string of characters 1 character long starting from position i. And i is incremeneted by one (i++) each time the for loop repeats.
Edited by ShadowValence, 30 August 2012 - 01:17 PM.
#7 Members - Reputation: 3329
Posted 24 August 2012 - 01:53 PM
Also I would like to ask, how long did it take you guys to get the hang of programming?
Somewhere between 90 seconds and 12 years depending on your definition of 'hang of it'.
It didn't really take long (~ week) to avoid many of these looping bugs, but it took a while (~ 4-6 years) before I stopped occasionally being 'blind' to this sort of error without a debugger.
That's key though. This sort of error would take about 5 seconds to identify if you stepped through the code with a debugger. Are you unfamiliar with your debugger, or were just looking at the wrong spot or what?
#8 Members - Reputation: 143
Posted 24 August 2012 - 05:49 PM
Are you unfamiliar with your debugger, or were just looking at the wrong spot or what?
Very unfamiliar, as I said in my topic post I missed a lot of time and work at university due to being in hospital for half of the year. Due to this I never got the hang of programming and only just passed my first year, I'm having to start from the beginning all over again because of the time I've missed and never caught up so honestly speaking I know very little about what I'm doing.
#10 Members - Reputation: 3329
Posted 24 August 2012 - 08:16 PM
Very unfamiliar, as I said in my topic post I missed a lot of time and work at university due to being in hospital for half of the year.
Yup, stuff happens. Now though, you know what sort of problems can be mitigated through the use of a debugger. Getting experience with it will provide immediate dividends.
#12 Moderators - Reputation: 6645
Posted 25 August 2012 - 09:41 AM
for (int i = 0; i < strText.Length; i++) {
if (strText[i] == 'g') {
LetterCount++;
}
}
Also C# supports foreach loops that look at each element of a collection without needing to manually extract the elements via an index or whatever.
foreach (char letter in strText) {
if (letter == 'g') {
LetterCount++;
}
}
Of course, there are a lot of other ways to write this, but many of them would only constitute showing off rather than efficient solutions.






