I guess I'm a huge c# noob, but I was wondering if you guys had any suggestions. I need to write a program that counts the number of "A"s in an input field. I wrote a loop that (I thought) would accomplish this by using the indexof("a") on the input string. As long as the return of indexof is greater than -1, I have the loop set to continue. Each time the loop runs, it is set to increase the variable "aCharCount" by one, with "aCharCount" being the total number of "A"s. That all seems to work, but I have to remove an "A" from the string each time so I don't continue to reference the same one over and end up in an infinite loop. For whatever reason, the remove method on the string is not working. I know my syntax for the method is right because if I remove the loop it will cycle through once. Any suggestions? Thanks in advance.
Code:int aCharCount = 0; string userString = txtInput.Text; int number = userString.IndexOf("a"); for (number = userString.IndexOf("a"); number > -1; userString.Remove(number, 1);) { aCharCount++ } txtOutput.Text = aCharCount.ToString();
+ Reply to Thread
Results 1 to 8 of 8
-
This plan is so bad, it must be one of ours.
-
Wouldn't recommend this approach.....try this one:
string userString = txtInput.Text;
int aCharCount = 0;
for (int n=0; n < userString.Length; n++)
{
if (userString[n] == "a")
aCharCount++;
}
txtOutput.Text = aCharCount.ToString();
Andy. -
Your understanding of the for loop is wrong - the first part of the for (the IndexOf call in your case) only gets done ONCE at the beginning, not each time thru the loop. Try something like
count = 0;
for (number = IndexOf ; <= the initial setting
number > -1 ; <= repeat condition
number = IndexOf ) <= reset after each pass
{
Remove ; <= take out the one we just found
count++ ; <= count how many we found
} -
Thanks for the responses. I guess I didn't get the for loop all that well. So the third section in the parentheses is executed at the end of each loop cycle?
This plan is so bad, it must be one of ours. -
a simple for loop:
Code:for (i=0; i<100; i++) { body; }
Code:i=0; while(i<100) { body; i++; }
-
That makes sense, is there any point in declaring the variable inside the loop with a " for" as oppose to declaring it outside with a "while"?
This plan is so bad, it must be one of ours. -
The for() construct puts all the conditions in the same place.
for (starting condition; end test condition; stepping condition) -
**In addition to waltzingandy's code**
He left out the remove and for this code adding a ToLower would make it more solid in cases of capital A's.
string userString = txtInput.Text.ToLower();
int aCharCount = 0;
for (int n=0; n < userString.Length; n++)
{
if (userString[n] == 'a')
{
aCharCount++;
userString = userString.Remove(n--,1);
}
}
That should do italso notice minor changes, single quote instead of double quotes since you are using char. Also when removing, remember to lower the index so you dont eventually skip if there is 2 A's in a word and running into Array out of index exception error.
You'll need to reassign userString = .... because Remove method returns a string, using it by itself won't do you any good.
Similar Threads
-
Basic Issue - want to make video loop using DVD Studio Pro
By Donny Rowles in forum Authoring (DVD)Replies: 2Last Post: 19th Sep 2011, 13:56 -
How to loop avi file
By Nagaraj in forum Newbie / General discussionsReplies: 0Last Post: 12th Mar 2011, 07:17 -
i'm out of the loop..could someone update me?
By 91rtstealth in forum MacReplies: 6Last Post: 6th Nov 2008, 17:48 -
Basic Loop Trouble
By nigelgourley in forum Authoring (DVD)Replies: 1Last Post: 21st Nov 2007, 16:31 -
Help With Loop - PLEASE - Pinnacle Studio 9
By Cher4Scuba in forum EditingReplies: 1Last Post: 17th Aug 2007, 21:33