Get expert advice and insights on any topic with IDNLearn.com. Our platform is designed to provide accurate and comprehensive answers to any questions you may have.

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. Ex: If the input is: n Monday, the output is: Ex: If the input is: z Today is Monday, the output is: Ex: If the input is: n It's a sunny day, the output is Case matters. Ex: If the input is: n Nobody, the output is: n is different than N LAB ACTIVTY 4.12.1: LAB: Count characters 6/10 LAB ACTIVTY 4.12.1: LAB: Count characters 6/10 main.cpp Load default template... 1 #include«iostream» 2 using namespace std; 3 int mainO t 4 5 char C; 6 cin c 8 string s; 9 getline(cin, s); 10 11 int ct-0; 12 13 while (s. find(c) != string ::npos ) { 14 ct ct + 1; 15 s = s, find (c, s. find(c) + 1); 16 17 cout << ct << endl; 18 return 0: Total score: 6/10 Latest submission- 3:56 AM on 04/30/19 Only show failing tests Download this submission 1: Compare output 2/2 Input n Monday Your output 1 2: Compare output 2/2 Input z Today is Monday Your output 0 0/2 : Compare output Output differs. See highlights below. Input n It's a sunny day Your output Expected output2 4: Compare output 0/2 Output differs. See highlights below Input t this is a sentence with many t's Your output Expected output 2/2 5: Compare output Input x X Your output 0

Sagot :

Answer:

Following are the code to the given question:

#include<iostream>//including the header file  

using namespace std;

int count_Characters(char ch, string s)//defining a method count_Characters that holds two variable as a parameter

{

int c=0,i;//defining integer variable

for(i=0;i<s.size();i++)//using for loop to check value

{

   if(s[i]==ch)//use if block to check string and char value

   {

       c=c+1;//incrementing c value

   }

}

return c;//return c value

}

int main() //defining main method

{

char ch;//defining char variable

cin>>ch;//input char value

string s;//defining string variable

cin>>s;//input string value

cout<<count_Characters(ch,s);//calling method count_Characters

return 0;

}

Output:

n Nobody

0

n Monday

1

Explanation:

In this code, a method "count_Characters" is declared that accepts one sting and one char variable as a parameter "s, ch", and inside the method a two integer variable and for loop is declared that uses the if block to match string and char value and increments the c variable value.

In the main method two-variable "s, ch" is declared that inputs the value from the user-end and pass the value into the method, and prints its values.