Question

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 be:

int findLetters( string s, char valueA, char valueB, bool & foundBoth );

Here are some examples of how a main routine could test this function:

char valueA = ‘9’;
char valueB = ‘0’;
bool b = false;

assert( findLetters( “Hello World”, valueA, valueB, b ) == 0 );
assert( b == false );

assert( findLetters( “9876543210”, valueA, valueB, b ) == 2 );
assert( b == true );

assert( findLetters( “999888777”, valueA, valueB, b ) == 3 );
assert( b == false );

assert( findLetters( “”, valueA, valueB, b ) == -1 );
assert( b == false );

Write the findLetters function.

Homework Answers

Answer #1

Step 1 : Save the following code in AssertChar.cpp

#include <iostream>
#include <string>
#include <assert.h>
using namespace std;

int findLetters(string s, char valueA, char valueB, bool& foundBoth) {
if (s.length() <= 0) {
return -1;
}
char ch;
int result = 0;
bool foundA = false;
bool foundB = false;
for (int i = 0, len = s.length(); i < len; i++) {
ch = s.at(i);
if (valueA == ch) {
result++;
foundA = true;
}
if (valueB == ch) {
result++;
foundB = true;
}
}
foundBoth = foundA && foundB;
return result;
}

int main(int argc, char** argv) {
char valueA = '9';
char valueB = '0';
bool b = false;

assert(findLetters("Hello World", valueA, valueB, b) == 0);
assert(b == false);

assert(findLetters("9876543210", valueA, valueB, b) == 2);
assert(b == true);

assert(findLetters("999888777", valueA, valueB, b) == 3);
assert(b == false);

assert(findLetters("", valueA, valueB, b) == -1);
assert(b == false);
   return 0;
}

It will compile and run without any error.

It will not produce any output since all assert() will succeed.

If it fails, please send me the error message.

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 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.
javascript 1.Write a function delay that accepts two arguments, a callback and the wait time in...
javascript 1.Write a function delay that accepts two arguments, a callback and the wait time in milliseconds. Delay should return a function that, when invoked waits for the specified amount of time before executing. HINT - research setTimeout(); 2.Create a function saveOutput that accepts a function (that will accept one argument), and a string (that will act as a password). saveOutput will then return a function that behaves exactly like the passed-in function, except for when the password string is...
Java Program Implement a class called AnimalTrainer. Include the following data types in your class with...
Java Program Implement a class called AnimalTrainer. Include the following data types in your class with the default values denoted by dataType name : defaultValue - String animal : empty string - int lapsRan : 0 - boolean resting : false - boolean eating : false - double energy : 100.00 For the animal property implement both getter/setter methods. For all other properties implement ONLY a getter method Now implement the following constructors: 1. Constructor 1 – accepts a String...
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.
Using C++ Write a template function that accepts an integer parameter and returns its integer square...
Using C++ Write a template function that accepts an integer parameter and returns its integer square root. The function should return -1, if the argument passed is not integer. Demonstrate the function with a suitable driver program .
C++ Coding Please. Thank you! 1) Complete a function, getStringInfo. Two given character strings are as...
C++ Coding Please. Thank you! 1) Complete a function, getStringInfo. Two given character strings are as below. char *str1 = “my name is Amy!”; char *str2 = “I got lost on my way to work”; The function accepts two constant character strings, str1 and str2 as pointer. Exchange the sentence of the variables str1 and str2 and then compute and display both strings’ length using pointer. Return the sum of the lengths of str1 and str2 int getStringInfo(const char *str1,...
use c language only to solve this porblem -------------------------- Have the function StringChallenge(str) take the str...
use c language only to solve this porblem -------------------------- Have the function StringChallenge(str) take the str string parameter being passed and return the number of words the string contains (e.g. "Never eat shredded wheat or cake" would return 6). Words will be separated by single spaces. Examples Input: "Hello World" Output: 2 Input: "one 22 three" Output: 3 you have the following code what edits can you do #include <stdio.h> #include <string.h> void StringChallenge(char str[]) {      // code goes...
[Java] I'm not sure how to implement the code. Please check my code at the bottom....
[Java] I'm not sure how to implement the code. Please check my code at the bottom. In this problem you will write several static methods to work with arrays and ArrayLists. Remember that a static method does not work on the instance variables of the class. All the data needed is provided in the parameters. Call the class Util. Notice how the methods are invoked in UtilTester. public static int min(int[] array) gets the minimum value in the array public...
Using c++ Valid Palindrome In this assignment, you need to implement a bool isPalindrome(string s) function....
Using c++ Valid Palindrome In this assignment, you need to implement a bool isPalindrome(string s) function. Given a string s, isPalindrome(s) can determine if s is a palindrome, considering only alphanumeric characters and ignoring cases. Note: for the purpose of this problem, we define empty string as valid palindrome. Example 1: Input: ”A man, a plan, a canal: Panama” Output: true Example 2: Input: ”race a car” Output: false Requirement: There are many methods to check if a string is...
Java Please [(1)] A palindrome is a string that reads the same forwards as backwards. Using...
Java Please [(1)] A palindrome is a string that reads the same forwards as backwards. Using only a fixed number of stacks and queues, the stack and queue ADT functions, and a fixed number of int and char variables, write an algorithm to determine if a string is a palindrome. Assume that the string is read from standard input one character at a time. The algorithm should output true or false as appropriate [(2)] Let Q be a non-empty queue,...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT