How to write below code in C? Also: is there any built in function for checking length of an array?
Python Code
x = ['ab', 'bc' , 'cd']
s = 'ab'
if s in x:
//Code
解决方案
There is no function for checking length of array in C. However, if the array is declared in the same scope as where you want to check, you can do the following
int len = sizeof(x)/sizeof(x[0]);
You have to iterate through x and do strcmp on each element of array x, to check if s is the same as one of the elements of x.
char * x [] = { "ab", "bc", "cd" };
char * s = "ab";
int len = sizeof(x)/sizeof(x[0]);
int i;
for(i = 0; i < len; ++i)
{
if(!strcmp(x[i], s))
{
// Do your stuff
}
}