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 locate(const string a[], int n, string target);
Return the position of the first string in the array that is equal to target. Return 1 if there is no such string. As noted above, case matters: Do not consider \"jOn\" to be equal to \"Jon\".
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
Hi, Please find my code.
Please let me know in case of any issue.
1)
int locate(const string a[], int n, string target){
// base case, if size of array is passed negative value
if( n < 0){
return -1;
}
for(int i=0; i<n; i++){
// if current element of \'a\' is equal to \'target\', then return \'i\'
if(a[i].compare(target) == 0){
return i;
}
}
// reach here, meean no string in \'a\' is equal to \'target\'
return -1;
}
2)
int locate(const string a[], int n, string target){
// base case, if size of array is passed negative value
if( n < 0){
return -1;
}
int count = 0; // to store the count of strings that are equal to \'target\'
for(int i=0; i<n; i++){
// if current element of \'a\' is equal to \'target\', then return \'i\'
if(a[i].compare(target) == 0){
count++;
}
}
return count;
}

