How to write below code in C? Also: is there any built in function for checking length of an array?
如何用C写下面的代码?另外:是否有用于检查数组长度的内置函数?
Python Code
Python代码
x = ['ab', 'bc' , 'cd']
s = 'ab'
if s in x:
//Code
4 个解决方案
#1
12
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
在C中没有用于检查数组长度的函数。但是,如果声明的数组与要检查的数组的范围相同,则可以执行以下操作
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.
你必须遍历x并对数组x的每个元素执行strcmp,以检查s是否与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
}
}
#2
7
Something like this??
这样的事情?
#include <stdio.h>
#include <string.h>
int main() {
char *x[] = {"ab", "bc", "cd", 0};
char *s = "ab";
int i = 0;
while(x[i]) {
if(strcmp(x[i], s) == 0) {
printf("Gotcha!\n");
break;
}
i++;
}
}
#3
0
There is a function for finding string length. It is strlen
from string.h
有一个查找字符串长度的函数。它是来自string.h的strlen
And then you could use the strcmp
from the same header to compare strings, just as the other answers say.
然后你可以使用同一标题中的strcmp来比较字符串,就像其他答案所说的那样。
#4
0
A possible C implementation for Python's in
method could be
Python方法的一个可能的C实现可能是
#include <string.h>
int in(char **arr, int len, char *target) {
int i;
for(i = 0; i < len; i++) {
if(strncmp(arr[i], target, strlen(target)) == 0) {
return 1;
}
}
return 0;
}
int main() {
char *x[3] = { "ab", "bc", "cd" };
char *s = "ab";
if(in(x, 3, s)) {
// code
}
return 0;
}
Note that the use of strncmp
instead of strcmp
allows for easier comparison of string with different sizes. More about the both of them in their manpage.
请注意,使用strncmp而不是strcmp可以更轻松地比较不同大小的字符串。更多关于他们在他们的联机帮助页中的内容。
#1
12
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
在C中没有用于检查数组长度的函数。但是,如果声明的数组与要检查的数组的范围相同,则可以执行以下操作
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.
你必须遍历x并对数组x的每个元素执行strcmp,以检查s是否与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
}
}
#2
7
Something like this??
这样的事情?
#include <stdio.h>
#include <string.h>
int main() {
char *x[] = {"ab", "bc", "cd", 0};
char *s = "ab";
int i = 0;
while(x[i]) {
if(strcmp(x[i], s) == 0) {
printf("Gotcha!\n");
break;
}
i++;
}
}
#3
0
There is a function for finding string length. It is strlen
from string.h
有一个查找字符串长度的函数。它是来自string.h的strlen
And then you could use the strcmp
from the same header to compare strings, just as the other answers say.
然后你可以使用同一标题中的strcmp来比较字符串,就像其他答案所说的那样。
#4
0
A possible C implementation for Python's in
method could be
Python方法的一个可能的C实现可能是
#include <string.h>
int in(char **arr, int len, char *target) {
int i;
for(i = 0; i < len; i++) {
if(strncmp(arr[i], target, strlen(target)) == 0) {
return 1;
}
}
return 0;
}
int main() {
char *x[3] = { "ab", "bc", "cd" };
char *s = "ab";
if(in(x, 3, s)) {
// code
}
return 0;
}
Note that the use of strncmp
instead of strcmp
allows for easier comparison of string with different sizes. More about the both of them in their manpage.
请注意,使用strncmp而不是strcmp可以更轻松地比较不同大小的字符串。更多关于他们在他们的联机帮助页中的内容。