Skip to content
Related Articles

Related Articles

Improve Article

GATE | Gate IT 2005 | Question 53

  • Last Updated : 28 Jun, 2021

The following C function takes two ASCII strings and determines whether one is an anagram of the other. An anagram of a string s is a string obtained by permuting the letters in 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;
}

Choose the correct alternative for statements A and B.
(A) A : count [a[j]]++ and B : count[b[j]]–
(B) A : count [a[j]]++ and B : count[b[j]]++
(C) A : count [a[j++]]++ and B : count[b[j]]–
(D) A : count [a[j]]++and B : count[b[j++]]–

Answer: (D)
Explanation:




#include <stdio.h>
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;
}

Quiz of this Question
Please comment below if you find anything wrong in the above post

Attention reader! Don’t stop learning now.  Practice GATE exam well before the actual exam with the subject-wise and overall quizzes available in GATE Test Series Course.

Learn all GATE CS concepts with Free Live Classes on our youtube channel.

My Personal Notes arrow_drop_up
Recommended Articles
Page :