127k views
2 votes
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.

User Paul Byrne
by
3.3k points

2 Answers

2 votes

Answer:

text = input()

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

Step-by-step explanation:

User Glevine
by
3.4k points
2 votes

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);

}

}

Step-by-step 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
User Waleed Mohsin
by
3.4k points