Ask Question
3 January, 17:18

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

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.

Ex: If the input is: n Nobody

the output is: 0 n is different than N.

+1
Answers (1)
  1. 3 January, 19:11
    0
    import java. util. Scanner;

    public class num4 {

    public static void main (String[] args) {

    Scanner in = new Scanner (System. in);

    System. out. println ("Enter a word");

    String word = in. nextLine ();

    System. out. println ("enter a character");

    char c = in. nextLine (). charAt (0);

    int count=0;

    for (int i = 0; i < word. length (); i++) {

    if (word. charAt (i) = =c) {

    count++;

    }

    }

    System. out. println (count);

    }

    }

    Explanation:

    Implemented in Java Prompt and receive user input for a string and a character Use a for loop to iterate through each character of the string Within the for loop check for the occurrence of the character and increment the variable count already initialized Using Java Programming Language Outside the for loop print count
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Write a program whose input is a string which contains a character and a phrase, and whose output indicates the number of times the ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers