String Comparison Discrepancy (Works for some cases and sometimes doesn't)
我使用以下代码测试了我收到的电子邮件文件中的一些文本检测。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | #include <stdio.h> int main() { int max_size = 100; char email[100] ="aheanstring4strinstringzil.comg"; int pointer = 0; int i = 1; int j = 0; int len = 0; int chk = 0; char found1[7] ="string1"; char found2[7] ="string2"; char found3[7] ="string3"; char found4[7] ="string4"; char found5[7] ="string5"; char user1[7] ="string1"; // 23 char user2[7] ="string2";// 19 char user3[7] ="string3"; // 14 char user4[7] ="string4"; // 16 char user5[7] ="string5";; // 15 while (pointer != max_size && chk != 1) { for (j = 0;j<7; j++) { found1[j] = *(email+pointer+j); } if (strcmp(found1, user1) == 0){ printf("Authorized User Found\ "); chk = 1; continue; } for (j = 0;j<7; j++) { found2[j] = *(email+pointer+j); } if (strcmp(found2, user2) == 0){ printf("Authorized User Found\ "); chk = 1; continue; } for (j = 0;j<7; j++) { found3[j] = *(email+pointer+j); } if (strcmp(found3, user3) == 0){ printf("Authorized User Found\ "); chk = 1; continue; } for (j = 0;j<7; j++) { found4[j] = *(email+pointer+j); } if (strcmp(found4, user4) == 0){ printf("Authorized User Found\ "); chk = 1; continue; } for (j = 0;j<7; j++) { found5[j] = *(email+pointer+j); } if (strcmp(found5, user5) == 0){ printf("Authorized User Found\ "); chk = 1; continue; } pointer++; } printf("Check is %d, Pointer is %d\ ",chk, pointer); return 0; } |
我使用上述代码在电子邮件正文中查找特定用户。如果找到用户,while 循环将中断。当我尝试运行它时,我在上面的变量 (email)
中包含了不同的字符串
我首先尝试在不同的在线 C 编译器上运行它。他们都有字符串 1,3 和 5 工作正常。 (被检测到)
其中一些字符串 2 工作正常(被检测到)。
然而,他们都分享了 string2 从未被检测到的事实。我不知道为什么。我试着想一个原因,但不知道为什么。
非常感谢您的帮助。
1 | char found1[7] ="string1"; |
这里
1 | char found1[8] ="string1"; |
您将
或者正如@Barak Manos 建议的那样,你可以去
1 | char found1[] ="string1"; |