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.

Respuesta :

ijeggs

Answer:

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

Answer:

text = input()

print(text[1::].count(text[0]))

Explanation: