In: Computer Science
Please write the code in c++
Write a function with one input parameter that is a vector of strings. The function should count and return the number of strings in the vector that have either an 'x' or a 'z' character in them.
For example, when the function is called, if the vector argument contains the 6 string values, "enter", "exit", "zebra", "tiger", "pizza", "zootaxy" the function should return a count of 4. ("exit", "zebra", "pizza", and "zootaxy" all have an x and/or a z character.)
You may assume all letters in the vector of strings are lowercase and that the vector can have a large number of elements.
input code:
output 1:
code:
#include <bits/stdc++.h>
using namespace std;
int count_x_z(vector<string> input)
{
/*declare the variables*/
int count=0;
char ch1='x',ch2='z';
/*find the number of string contain x or z*/
for(int i=0;i<input.size();i++)
{
/*store string into k*/
string k=input[i];
int n=k.length();
/*check that string contain x or z or not*/
for(int j=0;j<n;j++)
{
/*if true than increment count*/
if(k[j]==ch1 || k[j]==ch2)
{
count++;
break;
}
}
}
/*return count*/
return count;
}
int main()
{
/* Declaring Vector of String type*/
vector<string> input={"enter", "exit", "zebra", "tiger",
"pizza", "zootaxy"};
/*find the total*/
int total=count_x_z(input);
cout<<"Number of x and z in vector is :"<<total;
}