Ask Question

Write a program whose input is a character and a string, and whose output indicates the number of times the character appears in the string.

Ex: If the input is: n Monday,

the output is: 1

Ex: If the input is: z Today is Monday,

the output is: 0

Ex: If the input is: n It's a sunny day,

the output is: 2

Case matters. n is different than N.

Ex: If the input is: n Nobody,

the output is: 0

Your program must define and call the following function that returns the number of times the input character appears in the input string.

int CountCharacters (char userChar, string userString)

+2
Answers (1)
  1. 21 March, 21:55
    0
    The program written in Java is given in the explanation section

    Explanation:

    public class num9 {

    //Defining the method CountCharacters ()

    public static int CountCharacters (char userChar, String userString) {

    int c = userString. length ();

    int count=0;

    for (int i=0; i
    if (userString. charAt (i) = =userChar) {

    count++;

    }

    }

    return count;

    }

    //Main method begins here

    public static void main (String[] args) {

    char n = 'n';

    String word = "Monday";

    //Calling the method CountCharacters ()

    System. out. println (CountCharacters (n, word));

    }

    }
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Write a program whose input is a character and a string, and whose output indicates the number of times the character appears in the ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers