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
polet [3.4K]
3 years ago
6

8.11 LAB: Count characters - functions Write a program whose input is a character and a string, and whose output indicates the n

umber of times the character appears in the string. 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. n is different than N. Ex: If the input is: n Nobody, the output is: 0 Your program must define and call the following function that returns the number of times the input character appears in the input string. int CountCharacters(char userChar, const string
Computers and Technology
1 answer:
Shkiper50 [21]3 years ago
6 0

Answer:

#include<iostream>

using namespace std;

int CountCharacters(char userChar, const string inputstr){

   int k = 0;

   int iter = 0;

   for (iter = 0; iter < inputstr.size(); iter++){

       if (inputstr[iter] ==  userChar){

           ++k;        }}

   return k;}

int main(){

   string str;

   char userChar[1];

   cout<<"Char: ";    cin>>userChar;

   cin.ignore();

   cout<<"String: ";   getline(cin, str);

   cout<<CountCharacters(userChar[0],str);

   return 0;}

Explanation:

Written in C++:

The function is defined here:

int CountCharacters(char userChar, const string inputstr){

This initializes a count variable k to 0

   int k = 0;

This initializes an iterating variable iter to 0

   int iter = 0;

This iterates through the characters of the string being passed to the function

   for (iter = 0; iter < inputstr.size(); iter++){

This checks for matching characters

       if (inputstr[iter] ==  userChar){

If found,  k is incremented by 1

           ++k;        }}

This returns the total count

   return k;}

The main begins here

int main(){

This declares a string variable

   string str;

This declares a character variable

   char userChar[1];

This gets input for the character variable

   cout<<"Char: ";    cin>>userChar;

This lets the user get another input

   cin.ignore();

This gets input for the string variable

   cout<<"String: ";   getline(cin, str);

This calls the function and return the count of character in the string

   cout<<CountCharacters(userChar[0],str);

You might be interested in
What is a high level language?​
vovikov84 [41]

Answer:

a high level language is any programming language that enables development of a program in a much more user friendly programming context and is generally independent of the computers hardware architecture.

8 0
3 years ago
Read 2 more answers
What should the car be programmed to do if it encounters an unavoidable accident? (about autonomous vehicles)(help me :c)
sashaice [31]

Answer:

The nation proposed that: “self-driving cars should always attempt to minimize human death and shouldn't discriminate between individuals based on age, gender, or any factor. Human lives should also always be given priority over animals or property” (Nowak).

4 0
3 years ago
Polaroid had a unique competitive advantage for many years until it forgot to observe competitive intelligence. The firm went ba
Snowcat [4.5K]

Answer:

false

Explanation:

Based on the information provided within the question it can be said that the statement being made is false. This statement does not exemplify Porter's threat of new entrants, since this talks about the threat of new companies entering the same market with the same product in order to compete with the existing competitors in that market. In this scenario the new companies that eventually made Polaroid Bankrupt had an innovative new technology and which people starting using and left the entire Polaroid Market behind.

8 0
3 years ago
A(n) _____ chart is drawn on the same worksheet as the data.
svet-max [94.6K]
A is the answer because it makes more sense.............
5 0
3 years ago
What happens when you position the mouse cursor over an edge or corner of a bounding box that has sizing handles?
bezimeni [28]

Answer:

The cursor changes to a two edged arrow pointer and the bounding box is highlighted.

Explanation:

Bounding boxes in windows operating system is a boundary line of the windows box that run that runs applications and other utilities in the system. A sizing handle is a tool used in place of a the minimize and maximize button on top of the windows box. It is found on the bottom right edge of the windows box.

When a cursor points to the sizing handle, the cursor changes to a double edge arrow pointer and the bounding box is highlighted, and can be resized on the screen.

7 0
3 years ago
Read 2 more answers
Other questions:
  • List the different generation of computers with its main component​
    8·1 answer
  • Carla needs to list the role for each consultant. Those with four or more years of 26 15 experience take the Lead role. Otherwis
    9·1 answer
  • The central computer on a network is called the ______. A) boss B) client C) parent D) server
    11·2 answers
  • Design a class named Person with fields for holding a person’s name, address, and telephone number. Write one or more constructo
    12·1 answer
  • Which type of element is , and what is the meaning of that element?
    8·1 answer
  • What are the most positive and the most negative decimal numbers that can be represented by a 2C (n+k) bit fixed-point number, w
    12·1 answer
  • To play a sound during transitions select a sound from - - - - - - list​
    11·1 answer
  • Let A and B be two stations attempting to transmit on an Ethernet. Each has a steady queue of frames ready to send; A’s frames w
    7·1 answer
  • Write a line of code that declares a variable called shipName and set it equal to the string SpaceX3000.
    9·1 answer
  • The ______________<br> holds data only while the computer is on.
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!