0

I am sorry if there is an answer out there because I can't really find it. I have a method here where when I pass in a phrase and a letter, it will check if there is that letter in the phrase. Originally it will print out false or true if there is that letter you chose in the phrase. My assignment wants me to change it so that it will only print out how many times that letter appears in the phrase. I couldn't really figure it out, and keep getting an error because my return statement is an int not a boolean.

public boolean findLetter(String letter, String text)
   {
       boolean flag = false;
       int count = 0;
       for(int i=0; i < text.length(); i++)
       {
           if (text.substring(i, i+1).equalsIgnoreCase(letter) )
               {
              
               count++;
               
              
               }
          
       }
       return count;
    }

1 Answer 1

0
  • First point, I guess you're working on C#.
  • Second point, if so, you have many code errors. The first one is boolean is capitalized, Boolean. Then the string.Length property has no parenthesis.
  • Third point, there is a property called string.Contains(char/string); that by itself returns a value of type bool.
  • Fourth point and last point: With the following code fragment check how many times a string or letter is repeated:
using System.Text.RegularExpressions;
string text = "ABABBA";
string letter = "AB"
public int FindLetter(string letter, string text)
{
int total = Regex.Matches(text, letter).Count;
return total;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.