CameraIcon
CameraIcon
SearchIcon
MyQuestionIcon
MyQuestionIcon
1
You visited us 1 times! Enjoying our articles? Unlock Full Access!
Question

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]]--
No worries! We‘ve got your back. Try BYJU‘S free classes today!
B
A : count [a[j]]++ and
B : count [b[j]]--
No worries! We‘ve got your back. Try BYJU‘S free classes today!
C
A : count [a[j]]++ and
B : count [b[j]]++
No worries! We‘ve got your back. Try BYJU‘S free classes today!
D
A : count [a[j]]++ and
B : count [b[j++]]--
Right on! Give the BNAT exam to get a 100% scholarship for BYJUS courses
Open in App
Solution

The correct option is D A : count [a[j]]++ and
B : count [b[j++]]--
Definition of anagram: a string s is a string obtained by permuting the letter in s.

1st for loop just create an array count and n initialize to 0 for each element i.e. for 128 element. While loop run till string reaches to end of strings. In while loop, for every element a[i] count is increased which is decreased by array b[i] when same element arrive in array b[i] and increment loop with j++.

If both strings are anagram of each other then the count value will be zero for each elements.

So, count[a[j]]++; and count[b[j++]]--;

Example:
Inside while loop

(1)count[g]++;(2)count[a]++;count[a];count[t];j++;j++;(3)count[t]++;(4)count[e]++;count[g];count[e];j++;j++;

While loop fail for (5)
Since atlast count will contain all zero. So both are anagram of each other.

flag
Suggest Corrections
thumbs-up
0
similar_icon
Similar questions
View More
Join BYJU'S Learning Program
similar_icon
Related Videos
thumbnail
lock
Evaluation of Determinants
MATHEMATICS
Watch in App
Join BYJU'S Learning Program
CrossIcon