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
ss7ja [257]
3 years ago
15

Write a program whose input is a character and a string, and whose output indicates the number of times the character appears in

the string. You may assume that the string does not contain spaces and will always contain less than 50 characters.Ex: If the input is:n Mondaythe output is:1Ex: If the input is:z TodayisMondaythe output is:0Ex: If the input is:n It'ssunnytodaythe output is:2Case matters.Ex: If the input is:n Nobodythe output is:0n is different than N.C++ Code:#include #include int main(void) {/* Type your code here. */return 0;}

Computers and Technology
1 answer:
Zina [86]3 years ago
6 0

Answer:

Here is the C++ program:

#include <iostream> // to include input output functions

using namespace std; // to identify objects like cin cout

int counter(string userString, char character) { //function counter

   int count = 0;   // counts the no of times a character appears in the string

   for (int i=0;i<userString.length();i++)  

// loop to move through the string to find the occurrence of the character

       if (userString[i] == character) //if characters is found in the string

           count++;   //counts the occurrence of the character in the string

   return count; }   //returns the no of times character occurs in the string

int main() { //start of the main() function body

   string s; // stores the string entered by the user

   cout<<"Enter a string: "; //prompts user to enter the string

   cin>>s; //reads the string from user

   char ch; //stores the character entered by the user

   cout<<"Enter a character: "; //prompts user to enter a character

   cin>>ch; //reads the character from user

   cout << counter(s, ch) << endl; }  

//calls counter function to find the number of times a character occurs in the //string

Explanation:

The counter function works as following:

It has a count variable which stores the number of occurrences of a character in the userString.

It uses a for loop which loops through the entire string.

It has i position variable which starts with the first character of the string and checks if the first character of userString matches with the required character.

If it matches the character then count variable counts the first occurrence of the character and in the userString and is incremented to 1.

If the character does not match with the first character of the userString then the loops keeps traversing through the userString until the end of the userString is reached which is specified by the length() function which returns the length of the string.

After the loop ends the return count statement is used to return the number of occurrences of the character in the userString.

The main() function prompts the user to enter a string and a character. It then calls counter() function passing string s and character ch arguments to it in order to get the number of times ch appears in s.

The output is attached in a screenshot.

You might be interested in
The advancements in which of the following technologies has most changed the American job market in recent years?
vodka [1.7K]

Answer:

Manufacturing mostly

7 0
3 years ago
Read 2 more answers
Difference between volatile and non volatile memory
slega [8]
<span>Volatile memory requires electricity or some kind of current to store information, and nonvolatile memory does not.</span>
6 0
3 years ago
Read 2 more answers
What is wrong with each of the following?
ankoles [38]

Answer:

The answer to this question is given below in the explanation section

Explanation:

shoNum + shoNum2 = shoTotal

in this code statement,  the order of operation is wrong because the calculation in programming always done at the right side of dependent variables.  so the correct statement of code is

<em> shoTotal = shoNum + shoNum2;</em>

shoNum = 10 * 4

This statement is programmatically correct, because the ShoNum value becomes 40 after performing the multiplication operation on its assigned value  (10 multiply by 4). However, it is noted that the empty space mostly ignored by the compiler. So, the correct is <em>shoNum = 10*4;</em>

Dim decPrice As Decimal = "$4.99"

The dollar sign is appeared in along with its value. So, if you assign value to a decimal variable, you need to assign only digit value (do not mix value with symbol or text) and also do not put quotation marks around value, if you put quotation marks around value then this value will be consider as a text value.

So, the correct statement is :

<em>Dim decPrice As Decimal = 4.99;</em>

shoCube= (shoSide)3

In this statement, cube is calculated of shoSide variable, mathematically it is right, but programmatically it is incorrect. Because, you missed the multiplication operation (*) between the operand shoSide and 3. it should

be as  shoSide raise to power 3.

the correct statement is : shoCube = (shoSide * shoSide *shoSide);

4 0
3 years ago
Which of the following commands would I use to begin a new presentation?
ki77a [65]

Answer:

begin >new

Explanation:

just trust me ok hehe

7 0
3 years ago
How to reinstall windows 7 on my pc,without format,because i don't want to loose my data,and can u give me a windows 7 setup ple
Tems11 [23]
You could install it from a flash drive a disk.
5 0
3 years ago
Other questions:
  • Write the definition of a method named printPowerOfTwoStars that receives a non-negative integer n and prints a string consistin
    10·1 answer
  • The work principle is not always true<br> T or F
    11·2 answers
  • Find the word-length 2's complement representation of each of the following decimal numbers:a. 845b. 15000c. 100d. -923
    8·2 answers
  • Operational feasibility, which refers to user acceptance and support, as well as management acceptance and support, is also know
    9·1 answer
  • Using 2 bytes, how many different characters can Unicode represent?
    14·1 answer
  • If you cause a car accident, which type of insurance will require you to pay the least out of pocket?
    5·2 answers
  • Integer indexing array: Weekend box office The row array movieBoxOffice stores the amount of money a movie makes (in millions of
    11·1 answer
  • Write a program that first reads in the name of an input file and then reads the input file using the file.readlines() method. T
    7·1 answer
  • Zachary drinks 2 cups of milk per day. He buys 6 quarts of milk. How many days will his 6 quarts of milk last?
    12·1 answer
  • What are the components of computer system??<br>​
    10·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!