How to check if a string is in an array of strings in C?

后端 未结 4 635
北海茫月
北海茫月 2021-01-01 19:06

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\'          


        
4条回答
  •  悲&欢浪女
    2021-01-01 19:51

    Something like this??

    #include 
    #include 
    
    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++;
        }
    }
    

提交回复
热议问题