161k views
0 votes
Functions with string parameters. C++

Complete the function ContainsSome() that has one string parameter and one character parameter. The function returns true if at least one character in the string is equal to the character parameter. Otherwise, the function returns false.
Ex: If the input is tzzq z, then the output is:
True, at least one character is equal to z.
----------------------
#include
using namespace std;

bool ContainsSome(string inputString, char x) {

/* Your code goes here */

}

int main() {
string inString;
char x;
bool result;

cin >> inString;
cin >> x;

result = ContainsSome(inString, x);

if (result) {
cout << "True, at least one character is equal to " << x << "." << endl;
}
else {
cout << "False, all the characters are not equal to " << x << "." << endl;
}

return 0;
}

User Keith Kong
by
7.9k points

1 Answer

1 vote

Answer:

Here is the completed code for the function ContainsSome():

#include <iostream>

using namespace std;

bool ContainsSome(string inputString, char x) {

for (int i = 0; i < inputString.length(); i++) {

if (inputString[i] == x) {

return true;

}

}

return false;

}

int main() {

string inString;

char x;

bool result;

cin >> inString;

cin >> x;

result = ContainsSome(inString, x);

if (result) {

cout << "True, at least one character is equal to " << x << "." << endl;

}

else {

cout << "False, all the characters are not equal to " << x << "." << endl;

}

return 0;

}

Step-by-step explanation:

The function ContainsSome() iterates through each character in the input string and checks if it is equal to the character parameter x. If a match is found, the function immediately returns true. If no matches are found, the function returns false after all characters have been checked.

User Dharma
by
8.9k points

No related questions found