Join the IDNLearn.com community and get your questions answered by knowledgeable individuals. Our platform offers reliable and detailed answers, ensuring you have the information you need.

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. The output should include the input character and use the plural form, n’s, if the number of times the character appears is not eactly 1.
Ex if the input is: n Monday
the output is: 1 n
Ex: if the input is: z Today is Monday
The output is 0 z’s
Ex: if the input is: n It’s a Sunday day
the output is: 2 n’s
case matters
Ex: if the input is: n Nobody
the output is: 0 n’s
N is different than N.


I need help putting it in Java


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 The Output Should Inc class=

Sagot :

Answer:

class Main {

 public static void analyzeString(char c, String s) {

   int count = 0;

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

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

       count++;

     }

   }

   

   System.out.printf("%c %s\n", c, s);

   System.out.printf("%d %c%s\n\n",count, c, count==1 ? "":"'s");

 }

 

 public static void main(String[] args) {

   analyzeString('n', "Monday");

   analyzeString('z', "Today is Monday");

   analyzeString('n', "It’s a Sunday day");

   analyzeString('n', "Nobody");

 }

}

Explanation:

I did not add code to prompt the user for input, but rather added the test cases as you provided.

By the way, there is only one 'n' in 'It's a Sunday day'. So the example is wrong there.

View image Tonb