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
Which of the following is a true statement about psychological tests administered by computers? computers make standardization e
Hitman42 [59]
Thank you for posting your question here at brainly. I hope the answer will help you. Feel free to ask more questions.
the true statement about psychological tests administered by computers is 
<span>most people find computerized tests to be difficult to understand. </span>
7 0
3 years ago
¿En qué países se ha implementado un sistema de vigilancia a través de drones?, ¿qué aspectos positivos y negativos ha generado?
mel-nik [20]

Answer:

17 países.

Explicación:

Hay diecisiete países que tienen vehículos aéreos no tripulados armados y donde se ha implementado un sistema de vigilancia con drones. Los drones son excelentes para recopilar grandes cantidades de datos de imágenes. Se puede implementar fácilmente para la vigilancia y mantener la seguridad. Se puede conducir a través de la computadora, no del piloto en él, por lo que el piloto se guarda. Los aspectos negativos de los drones es que tienen tiempos de vuelo cortos y el clima puede afectar fácilmente a los drones. Las operaciones precisas son difíciles y también eliminan futuros trabajos.

7 0
2 years ago
The Mark Table of Contents Entry option is used for marking nonheading text for the table of contents. the options are manually
enyata [817]

Answer:number 1

Explanation:asian culture

5 0
2 years ago
Read 2 more answers
Plz help meeeeee QUICKLY!!! WORTH 15 POINTS!
Lady bird [3.3K]

Answer:

Im pretty sure Domain name system but dont trust me

Explanation:

8 0
3 years ago
How computer can affect the life of people?
SCORPION-xisa [38]
<h2>Answer These are the negative effects</h2><h2>Low grades In school: others use the computer not for studying but something else. Which makes them unfocused in school.</h2><h2 /><h2>Waist and wrist pains: Due to long sitting ppl experience pains all over the body. </h2>

<h2>Addiction: People get addicted to the computer and makes them forget they have something important doing.</h2>
4 0
2 years ago
Other questions:
  • Changeover means that ____. (select all that apply)
    7·2 answers
  • The ____ is a new feature in versions of microsoft office, starting with office 2007; it consists of tabs, which contain groups
    5·1 answer
  • Tell me about how to build robots
    11·1 answer
  • If a user receives a message whose tone and terminology seems intended to invoke a panic or sense of urgency, it may be a(n) ___
    13·1 answer
  • 6. Why did he choose to install the window not totally plumb?
    13·1 answer
  • Creating a chart using a spreadsheet Chart Wizard involves four steps. Which is the last step?
    6·1 answer
  • If nothings faster than light then how do the dark get there first????
    5·2 answers
  • Write a programmer defined function that compares the ASCII sum of two strings. To compute the ASCII sum, you need to compute th
    6·1 answer
  • What the difference between an operating system drive and a storage drive?
    6·1 answer
  • Which of the following is not harmful to the computer?<br> a) Trojan<br> b) Rootkit<br> c)Malware
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!