+ 5
How to check an element to be in an array or not in C++?
listStr = ["hi", "hello", "bye"] elt = "hi" if elt in listStr : print("yes") else: print("no")
2 Answers
+ 1
//https://code.sololearn.com/cQwlCmY1xAPN/#cpp
#include <iostream>
using namespace std;
int main() {
string listStr[] = {"hi", "hello", "bye"};
string elt = "hi";
int x = 0;
for(int i = 0; i < sizeof(listStr) / sizeof(listStr[0]); i++) {
if (listStr[i] == elt) {
cout << "yes" << endl;
} else {
x++;
}
if (x == sizeof(listStr) / sizeof(listStr[0])) {
cout << "no" << endl;
}
}
return 0;
}
+ 1
tks