确定输入是否包含c中的算术运算符

时间:2015-08-27 21:25:21

标签: c

我想循环遍历数组arithchar中的字符,以确定输入的字符是否与之匹配。

我的代码如下:

The current character read is ''' with an int value of 39
expecting '}' or ',' but got current char ''' with an int value of 39
line number 1
index number 1

我需要帮助,特别是在确定我的最终 int checkForAO(char password_entered[]); int main(){ if(checkForAO(password_entered)){ //contains a password with ao } else{ //doesnt contain special ao. } int checkForAO(char password_entered[]){ int i; char arithchar[4] = {'+','-','*','/'}; for (i=0; i<strlen(password_entered); i++) { if ( <<<< password_entered[i] contains any character in arithchar array >>>>>>> ) ) { printf("\nYour password contain(s) ao.\n"); return true; } } printf("\nYour password didn't contain any ao.\n"); return false; } 声明时,我尝试伪造我需要的东西,但似乎无法弄明白这一点。

谢谢。

3 个答案:

答案 0 :(得分:4)

<string.h>

中使用strpbrk

E.g

int checkForAO(char password_entered[]){
    if(strpbrk(password_entered, "+-*/")){
        printf("\nYour password contain(s) ao.\n");
        return true;
    } else {
        printf("\nYour password didn't contain any ao.\n");
        return false;
    }
}

答案 1 :(得分:1)

我建议你做的是在if:

中添加以下一行
if ( password_entered[i] == '+' || password_entered[i] == '-' || password_entered[i] == '*' || password_entered[i] == '/') )

并删除以下行:

// char arithchar[4] = {'+','-','*','/'};

您的功能将变为:

 int checkForAO(char password_entered[]){
    int i;

    // char arithchar[4] = {'+','-','*','/'};
    for (i=0; i<strlen(password_entered); i++) {
        if ( password_entered[i] == '+' || password_entered[i] == '-' || password_entered[i] == '*' || password_entered[i] == '/') )
        {
            printf("\nYour password contain(s) ao.\n");
            return true;
        }
    }
    printf("\nYour password didn't contain any ao.\n");
    return false;

答案 2 :(得分:0)

        for (i=0; i<strlen(password_entered); i++) 
{
                    for(int j=0;j<strlen(arithchar);j++)
     {
              if(password_entered[i]==arithchar[j])
               {
                        printf("\nYour password contain(s) ao.\n");
                        return true;
               }
     }
}

会起作用。