Ask Question
19 May, 21:40

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.

+2
Answers (1)
  1. 20 May, 01:20
    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?
Get 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 & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers