VideoHelp Forum




+ Reply to Thread
Results 1 to 8 of 8
  1. 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();
    This plan is so bad, it must be one of ours.
    Quote Quote  
  2. 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.
    Quote Quote  
  3. Member
    Join Date
    Jun 2004
    Location
    Victoria, Australia
    Search Comp PM
    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
    }
    Quote Quote  
  4. 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.
    Quote Quote  
  5. a simple for loop:

    Code:
    for (i=0; i<100; i++)
    {
    	body;
    }
    is equivalent to:

    Code:
    i=0;
    while(i<100)
    {
    	body;
    	i++;
    }
    Quote Quote  
  6. 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.
    Quote Quote  
  7. The for() construct puts all the conditions in the same place.

    for (starting condition; end test condition; stepping condition)
    Quote Quote  
  8. Member nTekka's Avatar
    Join Date
    Jun 2007
    Location
    United States
    Search Comp PM
    **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 it also 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.
    Quote Quote  



Similar Threads

Visit our sponsor! Try DVDFab and backup Blu-rays!