C-检查字符串中的字符是否属于数组

C-检查字符串中的字符是否属于数组,c,arrays,string,loops,char,C,Arrays,String,Loops,Char,基本上,我试图做的是,当用户输入字符串时,我的代码将逐个检查字符,看看它们是否属于数组 For instance i have an array: char example[] = { 'a', 'b', 'c', 'd', 'e' }; 假设用户输入一个字符串“示例字符串” 现在我想分别检查字符串中的每个字符 如果它们存在于给定数组中。所以第一个字母“e”显然在数组中 而字母“x”在给定数组中不存在。到目前为止,我一直在努力 使用循环和memchr,但由于某些原因它无法工作,以下是我的代码:

基本上,我试图做的是,当用户输入字符串时,我的代码将逐个检查字符,看看它们是否属于数组

For instance i have an array:
char example[] = { 'a', 'b', 'c', 'd', 'e' };
假设用户输入一个字符串“示例字符串” 现在我想分别检查字符串中的每个字符 如果它们存在于给定数组中。所以第一个字母“e”显然在数组中 而字母“x”在给定数组中不存在。到目前为止,我一直在努力 使用循环和memchr,但由于某些原因它无法工作,以下是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(){
    char array[] = { 'a', 'b', 'c', 'd', 'e' };
    char input[40]; /*Reserved for the string*/
    int lengthofstring,i;
    scanf("%[^\n]s",input); /*This enables spaces in the input, and let's say 
    the string in this case is "example"*/
    lengthofstring=strlen(input);
    for (i=0;i<lengthofstring;i++){
        if (memchr(array,input[i],sizeof(array)){
            /* Now in this example input[i]="e", when i=0 and sizeof(array)=5*/
            printf("The letter %c does exist\n",input[i]);
        }
        else {
            printf("The letter %c does NOT exist\n",input[i]);
        }
    }
}
#包括
#包括
#包括
int main(){
字符数组[]={a',b',c',d',e'};
字符输入[40];/*为字符串保留*/
int lengthofstring,i;
scanf(“%[^\n]s”,input);/*这将启用输入中的空格,例如
本例中的字符串是“example”*/
lengthofstring=strlen(输入);

对于(i=0;i代码似乎对我有效。我确实编辑了问题中的代码,我可能无意中删除了该问题。请随意重新编辑并发布原始代码,我似乎无法回滚编辑

工作:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(){
    char array[] = { 'a', 'b', 'c', 'd', 'e' };
    char input[40]; /*Reserved for the string*/
    int lengthofstring,i;
    scanf("%[^\n]s",input); /*This enables spaces in the input, and let's say 
    the string in this case is "example"*/
    lengthofstring=strlen(input);
    for (i=0;i<lengthofstring;i++){
        if (memchr(array,input[i],sizeof(array))) {
            /* Now in this example input[i]="e", when i=0 and sizeof(array)=5*/
            printf("The letter %c does exist\n",input[i]);
        }
        else {
            printf("The letter %c does NOT exist\n",input[i]);
        }
    }
}

一个问题是缺少一个右括号,修复后它似乎可以工作。现在可以工作了,谢谢。我刚刚意识到数组中的字母之间有间距,一旦删除它们,代码就开始正常运行。恼人的小错误似乎会导致最大的问题。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(){
char array[] = { 'a', 'b', 'c', 'd', 'e' };
char input[40]; /*Reserved for the string*/
int lengthofstring,i;
scanf("%[^\n]s",input); /*This enables spaces in the input, and let's say 
the string in this case is "example"*/
lengthofstring=strlen(input);
for (i=0;i<lengthofstring;i++){
if (memchr(array,input[i],sizeof(array)){
/* Now in this example input[i]="e", when i=0 and sizeof(array)=5*/
printf("The letter %c does exist\n",input[i]);}
else {
printf("The letter %c does NOT exist\n",input[i]);}