我正在尝试比较C中的字母。
我的代码是:
char kel[100];
char check[7];
check is random 8 letter
printf("Please Enter the Word:");
scanf("%s", &kel);
for(int k = 0; k < 7; k++)
{
for(int j = 0; j < size; j++)
{
if(check[i] != kel[i])
{
printf("Different");
}
}
}
我想用kel单词检查随机字母。如果随机字母不包含在kel中,我想发出警告。
我该怎么做?
谢谢, 约翰
答案 0 :(得分:0)
也许
#include <string.h>
if(NULL==strpbrk(kel, check)){
printf("not include");
}
答案 1 :(得分:0)
int size = strlen(kel);
for(int k = 0; k < 8; k++)
{
int j;
for(j = 0; j < size; j++)
{
if(check[k] == kel[j])
{
break;
}
}
if(j == size)
printf("Warning: different!");
}
答案 2 :(得分:0)
不要使用魔术数字,希望这有帮助
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX 7
int main(void)
{
char kel[100] = {0};
char check[MAX];
int i, j;
printf("Please Enter the Word:");
scanf("%s", kel); /* You don't need &, kel is already a pointer */
srand((unsigned int)time(NULL));
printf("Your random is:");
for (i = 0; i < MAX; i++) {
check[i] = 31 + rand() % 96; /* This will give you printable ASCIIs */
putchar(check[i]);
}
putchar('\n');
for (i = 0; i < MAX; i++) {
for (j = 0; j < MAX; j++) {
if (check[i] == kel[j]) break;
}
if (j == MAX) printf("Different %c\n", check[i]);
}
return 0;
}