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
Given the following code fragment, how many times does the loop body execute? int laps = 50; int myNum = 1; do { myNum = myNum +
Jlenok [28]

Answer:

one

Explanation:

The loop is used to execute the part of code or statement again and again until the condition is not FALSE.

There are three types of loop in programming.

1. for loop

2.while loop

3. do-while loop

Do-while has one special property which makes it different from other loops.

The loop statement executes once if the condition of the loop is failed in the starting.

In do-while, the statement executes first and then while checking the condition.

let discuss the code:

initially, laps=50, myNum=1

then, the program executes the statement in the loop.

so, myNum = 1 + 2=3.

A value 3 is assign to the variable myNum.

laps = 50 + 1=51.

A value 3 is assigned to the laps variable.

then while checking the condition (51 <= 1) which is FALSE.

The program terminates the loop.

Therefore, the answer is one.

8 0
3 years ago
Host A is a PC, connected to switch SW1 and assigned to VLAN 1. Which of the following are typically assigned an IP address in t
svlad2 [7]

Answer:

Option A and Option D are the correct options .

Explanation:

The basic rule for deciding if both computer interfaces would be in a similar subnet is whether both interfaces are isolated from one another and a router. In order that can provide a path for hosts in each VLAN to transmit data to hosts outside that VLAN, a nearby router should link its LAN interface to the similar VLAN as hosts and get an address in the similar subnet as hosts.

So, There would not be a router separating most of the hosts in that similar VLAN on the same transition, therefore these hosts will also be in a similar subnet. Moreover, some PC, linked to the similar switch but rather in a separate VLAN, may allow its packets to travel through the router to Host A, and the IP address of Host A would have to be in a separate subnet that is not similar to this new host.

7 0
3 years ago
You just installed a new application but users cannot connect to the application remotely. Which feature should be checked to en
qwelly [4]

Answer:

Allow an app or feature through windows firewall

Explanation:

This one had me thinking for a second. But i believe this is the correct answer. Don't quote me though lol. First you must know what a feature is, this is the section on the left hand side when you are inside of this step for the lab. It is the first thing listed from withing this function. Hope this helps!!!

4 0
3 years ago
An automated search feature used by search engines to find results that match your search terms is called a spider or _______. A
Sphinxa [80]

The answer is C: Crawler

The job of a search crawler and a web spider is the same. Actually, a search engine has many different names, such as automatic indexers and web spiders. A crawler is a program that browses the World Wide Web systematically in order to provide updated data to the particular search engine. Its purpose is to create entries for a search engine index. A crawler works by getting a list of URL’s to visit and store. It gets copies which it stores to the search engine to later index.

4 0
3 years ago
Read 2 more answers
In which of the following situations is it probably better to use a fixed-size array over another more flexible option, like Arr
ddd [48]
D) a situation where you don’t know....
7 0
3 years ago
Other questions:
  • The term used to describe an electronic device, operating under the control of instructions stored in its own memory, that can a
    7·1 answer
  • April plans on starting a perfume business and decides she needs to first
    15·1 answer
  • Which of the following is the amount of space available on a screen to display information?
    8·1 answer
  • How to find determinant of ​
    15·1 answer
  • Grooves and polished surfaces on desert pebbles are most likely caused by
    10·1 answer
  • What is wrong with the following declaration? How should it be fixed?
    11·1 answer
  • Jane Estroisch works as a manager in a multidomestic firm. She focuses on the long-term questions facing the organization such a
    15·1 answer
  • Write a function named word_count that accepts a string as its parameter and returns the number of words in the string. A word i
    10·1 answer
  • Who is the founded the java computer language?​
    5·1 answer
  • Update thejavafile names to include your initials at the end, send .java file only.1. Completein-place heapSort, which takes an
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!