1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
ss7ja [257]
3 years ago
15

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. You may assume that the string does not contain spaces and will always contain less than 50 characters.Ex: If the input is:n Mondaythe output is:1Ex: If the input is:z TodayisMondaythe output is:0Ex: If the input is:n It'ssunnytodaythe output is:2Case matters.Ex: If the input is:n Nobodythe output is:0n is different than N.C++ Code:#include #include int main(void) {/* Type your code here. */return 0;}

Computers and Technology
1 answer:
Zina [86]3 years ago
6 0

Answer:

Here is the C++ program:

#include <iostream> // to include input output functions

using namespace std; // to identify objects like cin cout

int counter(string userString, char character) { //function counter

   int count = 0;   // counts the no of times a character appears in the string

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

// loop to move through the string to find the occurrence of the character

       if (userString[i] == character) //if characters is found in the string

           count++;   //counts the occurrence of the character in the string

   return count; }   //returns the no of times character occurs in the string

int main() { //start of the main() function body

   string s; // stores the string entered by the user

   cout<<"Enter a string: "; //prompts user to enter the string

   cin>>s; //reads the string from user

   char ch; //stores the character entered by the user

   cout<<"Enter a character: "; //prompts user to enter a character

   cin>>ch; //reads the character from user

   cout << counter(s, ch) << endl; }  

//calls counter function to find the number of times a character occurs in the //string

Explanation:

The counter function works as following:

It has a count variable which stores the number of occurrences of a character in the userString.

It uses a for loop which loops through the entire string.

It has i position variable which starts with the first character of the string and checks if the first character of userString matches with the required character.

If it matches the character then count variable counts the first occurrence of the character and in the userString and is incremented to 1.

If the character does not match with the first character of the userString then the loops keeps traversing through the userString until the end of the userString is reached which is specified by the length() function which returns the length of the string.

After the loop ends the return count statement is used to return the number of occurrences of the character in the userString.

The main() function prompts the user to enter a string and a character. It then calls counter() function passing string s and character ch arguments to it in order to get the number of times ch appears in s.

The output is attached in a screenshot.

You might be interested in
What is the utility of a lever?
Kamila [148]
Muy cosas.
 fazer una montanha russa andar,ativar una armadilha, ativar una puerta etc.
3 0
3 years ago
Read 2 more answers
This device is used to connect sections of large networks?
ryzh [129]
Monster energy boissss
6 0
3 years ago
Read 2 more answers
PLZ HELP I AM TIMED!!!!
Nataliya [291]

Answer:

The correct answer is A.

Explanation:

I put D from the answer above and got it wrong and it said A was the correct answer. haha :)

4 0
2 years ago
Read 2 more answers
COMO HA SIDO LA INNOVACION DE ESTE OBJETO TECNOLOGICO DURANTE SU EVOLUCIÓN
Marizza181 [45]

Answer:

Cual es el objeto tecnologico?

Explanation:

Si dices Cual,puede ser que te ayude.

3 0
3 years ago
How to write a self-analysis essay
Ray Of Light [21]

Self-assessment essay writing steps

Outline your thesis.

Describe your performance on recent projects.

Describe your strengths and weaknesses.

Describe your goals.

<h3>HOPE THIS ANSWER IS HELPFUL TO U :)</h3>
4 0
3 years ago
Other questions:
  • Which sparkline type is best for displaying trends in data changes over time?
    11·1 answer
  • Pointsfor a failover cluster, what type of network would you use to communicate with an iscsi device?
    15·1 answer
  • A job application is a form used to make a job request.<br> True<br> False
    8·2 answers
  • Computers store temporary Internet files in the Recycle Bin. These files take up space and slow down a computer. Which tool can
    10·1 answer
  • Truck drivers probably cannot see your vehicle if you cannot
    6·2 answers
  • Digital certificates can be used for each of these EXCEPT _____. A. to encrypt channels to provide secure communication between
    13·1 answer
  • i need to also do a algorithm where if the total is a even number 10 points are added to the score and if the total is odd then
    11·1 answer
  • Suppose Host A wants to send a large file to Host B. The path from Host A to Host B has three links, of rates R1 = 500 kbps, R2
    14·1 answer
  • Why is 0.3333333333333333 the output of print(1/6 + 1/6) in python?
    8·1 answer
  • Derive the three-dimensional transformation matrix for scaling an object by a scaling factor s in a direction defined by the dir
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!