Question

Q5. Code a function, named CountNumerics, that accepts a parameter of a string , named str1,...

Q5. Code a function, named CountNumerics, that accepts a parameter of a string , named str1, and returns the number of numeric characters (‘0’ .. ‘9’) + in the string. Helpful clues:   The string class has a function called length() that can be accessed by str1.length().   To access an individual character in the string; use string indexing (str1[i]) where I is an integer. Indexing of a string array starts at 0.

Homework Answers

Answer #1

Program

#include <iostream>
#include <string>
using namespace std;

//function count and return the number of numeric characters in str1
int CountNumerics(string str1)
{
int count = 0;
for(int i=0; i<str1.length(); i++)
{
if(str1[i]>='0' && str1[i]<='9')
count++;
}
return count;
}

//main function
int main()
{
//testing
string str1 = "abcd12345";
cout<<"The number of numeric characters: "<< CountNumerics(str1)<<endl;

return 0;
}

Output:
The number of numeric characters: 5

Solving your question and helping you to well understand it is my focus. So if you face any difficulties regarding this please let me know through the comments. I will try my best to assist you.
Thank you.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a function named "replacement" that takes a string as a parameter and returns an identical...
Write a function named "replacement" that takes a string as a parameter and returns an identical string except with every instance of the character "i" replaced with the character "n python
Write a method called countChar which accepts a string parameter and a character parameter and returns...
Write a method called countChar which accepts a string parameter and a character parameter and returns an integer, that is: int countChar (String s, char c) This method should count the number of occurrences of the character c within the string s, and return the count to the caller. Also write a main method that tests countChar. All of the print statements and user interaction belong in the main method, not in countChar.
8) Write Python code for a function called occurances that takes two arguments str1 and str2,...
8) Write Python code for a function called occurances that takes two arguments str1 and str2, assumed to be strings, and returns a dictionary where the keys are the characters in str2. For each character key, the value associated with that key must be either the string ‘‘none’’, ‘‘once’’, or ‘‘more than once’’, depending on how many times that character occurs in str1. In other words, the function roughly keeps track of how many times each character in str1 occurs...
In PYTHON please: Write a function named word_stats that accepts as its parameter a string holding...
In PYTHON please: Write a function named word_stats that accepts as its parameter a string holding a file name, opens that file and reads its contents as a sequence of words, and produces a particular group of statistics about the input. You should report the total number of words (as an integer) and the average word length (as an un-rounded number). For example, suppose the file tobe.txt contains the following text: To be or not to be, that is the...
Write Java code that attempts to call a method named bootUp() which takes a String parameter...
Write Java code that attempts to call a method named bootUp() which takes a String parameter and returns an integer value. The String parameter should be read from a file named "bootConfig.txt" and is the only value in the file. If a BootUpException is thrown, print the Exception object's message. If a DriverException occurs, call the reboot() method and rethrow the original DriverException object. If any other type of Exception is thrown, print a generic error message to the console....
C++ only: Please implement a function that accepts a string parameter as well as two character...
C++ only: Please implement a function that accepts a string parameter as well as two character arguments and a boolean parameter. Your function should return the number of times it finds the character arguments inside the string parameter. When both of the passed character arguments are found in the string parameter, set the boolean argument to true. Otherwise, set that boolean argument to false. Return -1 if the string argument is the empty string. The declaration for this function will...
Q1) Write a Python function partial_print, which takes one parameter, a string, and prints the first,...
Q1) Write a Python function partial_print, which takes one parameter, a string, and prints the first, third, fifth (and so on) characters of the strings, with each character both preceded and followed by the ^ symbol, and with a newline appearing after the last ^ symbol. The function returns no value; its goal is to print its output, but not to return it. Q2) Write a Python function called lines_of_code that takes a Path object as a parameter, which is...
Complete the program named Message that accepts a user’s message and determines whether it is short...
Complete the program named Message that accepts a user’s message and determines whether it is short enough for a social networking service that does not accept messages of more than 140 characters. If the message is less than 140 character, show The message is ok, else show The message is too long. The following picture shows an example output when you enter “Hello”. (10 points)               using static System.Console; class Message {            static void Main()            {                       ...
Write a function makeDoubles in c++, that takes an integer parameter called size and: allocates a...
Write a function makeDoubles in c++, that takes an integer parameter called size and: allocates a new array of that many double's initializes all elements to be 0 returns this array
Fill in the code for the function below called containsAvgValue. It takes as input a float...
Fill in the code for the function below called containsAvgValue. It takes as input a float array of any length and returns 1 if the array happens to contain a value that equals the average of all the values in the array or 0 otherwise. For example, when give the array [2.0, 2.0] it should return 1 since the average value is 2.0 and the array contains that value. To help you, I’ve included a function that computes the average...