-1

我正在尝试比较并在我的两个数组中查找匹配项,但它不起作用,任何人都可以帮忙吗?

const char spam[30][30] = {"cash bonus","earn money","fast cash","free access","free gift","free membership",
    "giveaway","million dollars","information you requested","act now","winner","you have been selected",
    "congratulations","lose weight","meet singles","no cost","no interest","social securtiy number","bonus",
    "ad","join millions","opt in","warranty","instant","prize","lowest price","get paid","get out of debt","one time","winning"};
    char resp[SIZE][SIZE];//response string array 
    printf("Enter an email to be checked for spam\n");
    scanf("%s", &resp);
    
    //check if there is a match
    for(int i = 0; i < 30; i++){
        for(int j = 0; j < 30; j++){
        resp[i][j] = tolower(resp[i][j]);//make it lowercase
        int match = strncmp(resp[i], spam[i] );//compare 
        if(match == 0){
            printf("This email is spam! :(\n");
        }else{
            printf("No match :) \n");
        }//if else
    }//for loop2
    }//for loop1
4

1 回答 1

0

你不能以这种方式真正实现你的目标。您的垃圾邮件库元素由 2 个以空格分隔的单词组成。但是您的输入只是每个resp[i]. (除了使用 逐字进行输入之外,没有简单的方法可以在 2D 数组中进行输入scanf()
因此,当您将单个单词resp[i]与双字字符串进行比较时cash bonus,它将始终返回false
是可以帮助您的指南。还有,我很想更新这个问题。有没有办法用字符串字典来识别垃圾邮件,比如spam二维数组?

于 2021-07-24T03:41:42.547 回答