📜  门|门 IT 2005 |第 53 题

📅  最后修改于: 2021-09-27 05:27:17             🧑  作者: Mango

下面的 C函数采用两个 ASCII字符串并确定一个是否是另一个的字谜。字符串s 的变位词是通过对 s 中的字母进行排列而获得的字符串。

int anagram (char *a, char *b) {
int count [128], j;
for (j = 0;  j < 128; j++) count[j] = 0;
j = 0;
while (a[j] && b[j]) {
A;
B;
}
for (j = 0; j < 128; j++) if (count [j]) return 0;
return 1;
}

为陈述 A 和 B 选择正确的选项。
(A) A : count [a[j]]++ 和 B : count[b[j]]–
(B) A : count [a[j]]++ 和 B : count[b[j]]++
(C) A : count [a[j++]]++ 和 B : count[b[j]]–
(D) A : count [a[j]]++ and B : count[b[j++]]–

答案: (D)
解释:

#include 
char a[100], b[100];
int main(void) {
 int flag;
  printf("Enter first string\n");
 gets(a);
  printf("Enter second string\n");
 gets(b);
  flag = anagram(a, b);
  if (flag == 1)
 printf("\"%s\" and \"%s\" are anagrams.\n", a, b);
 else
 printf("\"%s\" and \"%s\" are not anagrams.\n", a, b);
  return 0;
return 0;
}
int anagram (char *a, char *b) {
int count [128], j;
for (j = 0; j < 128; j++) count[j] = 0;
j = 0;
while (a[j] && b[j]) {
count [a[j]]++;
count[b[j++]]--;
}
for (j = 0; j < 128; j++) if (count [j]) return 0;
return 1;
}

这个问题的测验
如果您发现上面的帖子有任何错误,请在下面评论