Write a function in C that does the following The function m

Write a function in C++ that does the following:

The function must not use any function templates from the algorithms portion of the Standard C++ library.

int enumerate(const string a[], int n, string target);

Return the number of strings in the array that are equal to target. [Of course, in this and other functions, if n is negative, the paragraph above that starts \"Notwithstanding\" trumps this by requiring that the function return 1. Also, in the description of this function and the others, when we say \"the array\", we mean the n elements that the function is aware of.] As noted above, case matters: Do not consider \"jon\" to be equal to \"JoN\".

The function must write take at least two parameters: an array of strings, and the number of items the function will consider in the array, starting from the beginning.

Notwithstanding each function\'s behavior described below, all functions that return an int must return 1 if they are passed any bad arguments (e.g. a negative array size, or a position that would require looking at the contents of an element past the last element we\'re interested in). Unless otherwise noted, passing 0 to the function as the array size is not itself an error; it merely indicates the function should examine no elements of the array.

Solution

Here is the code for you:

#include <iostream>
using namespace std;
bool StringEquals(string x, string y)   //Returns true if 2 strings are equal. False otherwise.
{
if(x.length() != y.length())
return false;
for(int i = 0; i < x.length(); i++)
if(x.at(i) != y.at(i))
return false;
return true;   
}
int enumerate(const string a[], int n, string target)   //Counts the number of strings that match target in the array a.
{
if(n <= 0)       //If n is non-positive, i.e., the number of strings to be compared.
return -1;       //Return -1.
int count = 0;   //Assign 0 to count.
for(int i = 0; i < n; i++)   //For each string in the array a.
if(StringEquals(a[i], target))   //If it is equal to target.
count++;           //Increment count.
  
return count;     //Return count.
}

Write a function in C++ that does the following: The function must not use any function templates from the algorithms portion of the Standard C++ library. int e

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site