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]
2 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]2 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
Help me i'll brainiest if its right
Dominik [7]

Answer:

Your response is correct.

I hope this helped at all.

4 0
2 years ago
Read 2 more answers
What is the most powerful gpu or known as the graphics card in the world?
Sliva [168]
Although it's too early in the year to know what the best one is and there are many, in 2015 the most powerful GPUs were the EVGA GeForce Titan X, the Zotac GeForce GTX 980Ti and the <span>Gigabyte Radeon R9 Fury X.</span>
6 0
2 years ago
Read 2 more answers
File
Degger [83]

Explanation:

formula should add cells D3:07.

Empanadas & More

1

In cell D9 create a formula that calculates the total after a 10% disco

you need help understanding how to take a percentage off of a total

LINE TOTAL

2 MENU ITEM

3 Empanadas: Buffalo Chicken

4 Empanadas: Braised Short Rib

5 Empanadas: Fig and Goat Cheese

6 Sides: Black beans and rice

7

UNIT PRICE

$2.98

$2.98

$3.75

$1.98

QUANTITY

20

30

25

40

TAX

8

TOTAL

6 0
3 years ago
Which of the following scan only works if operating system’s TCP/IP implementation is based on RFC 793?
Shkiper50 [21]

Answer:NULL Scan

Explanation:RFC 793(Request for comments) is a type of RFC command labeled with the number 793 which can operate with the TCP protocol.They are sort of document form which is from IETF( Internet Engineering Task Force ).

The null scan is scanning protocol used by legal as well as illegal hackers for working in the transfer control protocol architecture. It is used for the identification of the the ports and holes in TCP servers.they can also have the negative impact if used by the illegal hackers.

5 0
2 years ago
How many bits are necessary for a binary representation (unsigned) of:
pshichka [43]

Answer:

Hi!

The answer to:

a. 6 bits.

b. 9 bits.

c. 26 bits.

Explanation:

a. For the states of the U.S.A, you need 50 or more combinations to represents each element.

<u>If you use 6 bits, the possible combinations are 2⁶ = 64.  </u>

b. For days in a year, you need 365 or more combinations to represents each element.

<u>If you use 9 bits, the possible combinations are 2⁹ = 512. </u>

c. For inhabitants of California, you need 36,457,549 or more combinations to represents each element.

<u>If you use 26 bits, the possible combinations are 2²⁶ = 67,108,864.</u> If you use 25 bits instead of 26, then you have 2²⁵ = 33,554,432 combinations. These possible combinations are not enough to represent each inhabitant.

4 0
3 years ago
Other questions:
  • To name a computed field, follow the computation with the word ____ and then the name you wish to assign to the field
    8·1 answer
  • What technology allows data to be stored in one place and be retrieved by many systems?
    7·1 answer
  • An individual is first with the network before they are authorized to access resources on the network A. countermeasure B. vulne
    11·1 answer
  • Given that two int variables, total and amount, have been declared, write a sequence of statements that: initializes total to 0
    14·1 answer
  • You recently discovered that Marketing1 can connect to Admin1, and Admin1 can connect to Marketing1, but neither of these comput
    11·1 answer
  • Which of the following hacker attacks against wireless networks are virtually undetectable?
    12·1 answer
  • Which statement below is true about how the functions of the digestive and circulatory system are connected.
    7·1 answer
  • Explain the Decision making statement​
    15·1 answer
  • The Internet was first used by which of the following institutions?
    7·1 answer
  • What causes the hidden node problem in a wireless local area network (wlan)?
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!