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
How many points do I need to be able to message people
hammer [34]

That is a good question

8 0
3 years ago
Read 2 more answers
. Alex discovered a bunch of SATA drives in a box at the office and needs to check the contents. What can he do so that Windows
ZanzabumX [31]

You have to make sure the BIOS boot is set to the normal hard drive first, then plug in the SATA cable and power properly

8 0
3 years ago
Einstein's famous equation states that the energy in an object at rest equals its mass times the squar of the speed of light. (T
musickatia [10]

Answer:

The complete program is as follows:

m_str = input('Input m: ')

mass = float(m_str)

e = mass * 300000000**2

print("e = ",e)

Explanation:

This is an unchanged part of the program

m_str = input('Input m: ')

This converts m_str to float

mass = float(m_str)

This calculates the energy, e

e = mass * 300000000**2

This is an unchanged part of the program

print("e = ",e)

4 0
3 years ago
You are to calculate the property tax for tax payers. You will ask the county tax clerk if they want to run the program and init
andre [41]

Answer:

Explanation:

The following code is written in Java. It creates the loop as requested and validates the information provided before continuing, if the information is invalid it prints "Invalid Information to the screen" and asks the user to enter the information again. Finally, outputing all the requested information at the end of the program. The code has been tested and the output can be seen in the attached image below.

import java.util.ArrayList;

import java.util.Arrays;

import java.util.Scanner;

class Brainly {

   public static void main(String[] args) {

       Scanner in = new Scanner(System.in);

       int totalValues = 0;

       int totalTaxes = 0;

       int count = 0;

       while(true) {

           int lotNumber, value;

           double tax;

           System.out.println("Would you like to add an Entry: 1=Yes 0=No");

           int answer = in.nextInt();

           if (answer == 1) {

               System.out.println("Please enter lot number: ");

               lotNumber = in.nextInt();

               if ((lotNumber >= 0) && (lotNumber <= 999999)) {

                   System.out.println("Please enter assessed value: ");

                   value = in.nextInt();

                   tax = value * 0.05;

                   System.out.println("Lot Number: " + lotNumber);

                   System.out.println("Property Value: " + value);

                   System.out.println("Property Tax: " + tax);

                   totalTaxes += tax;

                   totalValues += value;

                   count += 1;

               } else {

                   System.out.println("Invalid Lot Number");

               }

           } else {

               break;

           }

       }

       int average = totalTaxes / count;

       System.out.println("Total Property Values: " + totalValues);

       System.out.println("Total Property Taxes: " + totalTaxes);

       System.out.println("Average Property Tax" + average);

   }

}

8 0
2 years ago
I am having trouble figuring out if this is correct. Can someone please help me ASAP?
CaHeK987 [17]

Explanation:

The X's have been correctly placed in the chart

A surge suppressor, which is also commonly known as a surge protector, is an important equipment that helps prevent damage to electric components from an unexpected increase of voltage. Let’s assume that you are living in a county where the effective utility voltage is between 110 to 120 volts. Assume that the accepted peak voltage should not be anything higher than 170 volts. If there is a surge and this surge reaches peak levels of 170 volts and higher, surge suppressors should jump in and prevent the peak AC voltage from going above the threshold.

People tend to confuse line conditioners and surge suppressors. While surge suppressors protect components from surges, line conditioners are built to make up for the loss of voltage from the lines. It does so by storing some power energy. This stored energy can later be modified and used when the volts drops to the desired 110 volts.

A UPS has DC battery power that provides battery backup power when the electricity goes off or when the levels of voltage drop significantly. When the electricity goes off means that the voltage is at 0 volts and it is the work of the UPS to kick its backup power in.

6 0
3 years ago
Other questions:
  • What is one way for an entrepreneur to decrease risk?
    8·1 answer
  • HURRY UP !!!!!!’<br> Each packet is addressed to the recipient's .
    8·1 answer
  • An organization uses SAP financial management software to store accounting details and Microsoft CRM software to record customer
    12·1 answer
  • ________ programming is a method of writing software that centers on the actions that take place in a program.
    8·1 answer
  • Given the char variable c, write an expression that is true if and only if the value of c is not the space character .
    7·1 answer
  • I have a question about a hotel tv: So I’ve brought my Nintendo Switch and my dock and plugged it in to the tv. Everything seems
    13·1 answer
  • I made Pico with a Ray Gun (Next is Dad/Tankman)<br><br> Opinons?
    11·2 answers
  • What are the steps in preparing a bootable USB installer?​
    11·1 answer
  • Who are all the fnaf characters?
    7·1 answer
  • reagan's firm has not had to make large investments in computer or networking hardware or in personnel to maintain the hardware
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!