Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Implement the countNotMatching method below, which takes as input a string and a

ID: 3575066 • Letter: I

Question

Implement the countNotMatching method below, which takes as input a string and a character and returns how many characters in the string are NOT the supplied character. An example main method, as well as corresponding output, is supplied – you cannot change this method, and the output when the program is run must conform exactly to the sample output.

sample run: Other characters: 1 Other characters: 3 Other characters: 3 public static void main(String args[]) System. out.printf("Other characters: %d%n count NotMatching("Foo o')); System. out.printf("Other characters: %d%n countNotMatching("Bar", b')); System. out.printf("Other characters: %d%n countNotMatching("Baz!", B')); put your countNotMatching method here

Explanation / Answer

public static void main(String []args){
System.out.printf("Other Characters: %d%n", countNotmatching("Foo", 'o'));   
System.out.printf("Other Characters: %d%n", countNotmatching("Bar", 'b'));
System.out.printf("Other Characters: %d%n", countNotmatching("Baz!", 'B'));
}

public static int countNotmatching(String myStr, char myChar)
{
  
int source_len = myStr.length(); // find the length of the input string to loop through
int count = 0; // counter to count the number of characters that are not matching with
// input character
  
for(int i = 0; i < source_len; i++)
{
if( myStr.charAt( i ) != myChar ) // compare the character with each character
// in the input string
{
count++; // increment the counter if there is mismatch
}
}
  
return count;
}