python 判断字符串是否在数组中_如何检查字符串是否在C中的字符串数组中?

论坛 期权论坛 编程之家     
选择匿名的用户   2021-5-23 12:43   11   0

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

}

}

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP