edited by
12,015 views
56 votes
56 votes

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$.

  1. A: count [a[j]]++ and B: count[b[j]]--
  2. A: count [a[j]]++ and B: count[b[j]]++
  3. A: count [a[j++]]++ and B: count[b[j]]--
  4. A: count [a[j]]++ and B: count[b[j++]]--
edited by

6 Answers

Best answer
66 votes
66 votes

The answer is (D).

#include <stdio.h>

int main(void) {

return 0;
}

int anagram (char *a, char *b) {
/*
ASCII characters are of 7-bits 
so we use count array to represent all the ASCII characters 
(ranging 0-127)
*/
int count [128], j;

/*
so this loop will initialize count of all the ASCII characters to be 
0 (zero)
*/
for (j = 0;  j < 128; j++) count[j] = 0;	

j = 0;
/*
"a[j] && b[j]"  ensures that anagram returns 0 (false) in case both 
strings have different length. Because different length strings cannot
be anagram of each other
*/

/*
Logic:

Below while loop increments ASCII equivalent position for its occurence
in array 'a'in count array; and decrements ASCII equivalent position 
for its occurence in array 'b'in count array.

Example: a = "ISS" and b = "SIS"
ASCII equivalent of:
I - 73
S - 83

j = 0:	Statement A will increment count[ASCII of 'I']==>count[73] 
count[73] = 0 --> 1
Statement B will decrement count[ASCII of 'S'] ==> count[83]	
count[83] = 0 --> -1 and will increment j	j = 0 --> 1

j = 1:	Statement A will increment count[ASCII of 'S'] ==> count[83]	
count[83] = -1 --> 0 
Statement B will decrement count[ASCII of 'I'] ==> count[73]	
count[73] = 1 --> 0 and will increment j	j = 1 --> 2

j = 2:  Statement A will increment count[ASCII of 'S'] ==> count[83]	
count[83] = 0 --> 1 
Statement B will decrement count[ASCII of 'S'] ==> count[83]	
count[83] = 1 --> 0 and will increment j	j = 2 --> 3

*** END OF LOOP ***	

*/
while (a[j] && b[j]) {


A;	//count [a[j]]++

/*
Note: j will be increment after count[]-- will execute
Resource: http://www.c4learn.com/c-programming/increment-operator-inside-printf
*/
B; 	//count[b[j++]]--
}

/*
This loop checks that the number of occurences of the individual ASCII
characters is same or not.
If count[i] = 0 ---> same number of occurences for ASCII chracter i 
---> return 1 (true)

if count[i]!= 0 ---> different number of occurences for ASCII chracter i 
---> return 0 (false)
*/

for (j = 0; j < 128; j++) if (count [j]) return 0;
return 1;
}
edited by
27 votes
27 votes

Answer is (D)

A:  Increments the Count by 1 at each index that is equal to the ASCII value of the alphabet it is pointing at

B: Decrements the count by 1 at each index that is equal to the ASCII value of the alphabet it is pointing at . Also it increments the loop counter for next iteration.

If one string is permutation of other ,there would have been equal increments and decrements at each index of array and So count should contain zero at each index.that is what the loop checks at last and if any non-zero elements is found, it returns 0 indicating that strings are not anagram to each other.

16 votes
16 votes
I would like to answer this

An anagram of string mean you are just channging the arrangements of alphabets in string 1 to get a another string S2.

where no of occurrences of a  character in string S1 should be equal to no of occurrences of a character in string s2

. Supose if s1= abc

then s2=cba

Now if  i see code . an array of size 128 is nothing but a total of ASCII charcaters .we have initalized to zeros

now the main logic lies here is for every character that come first in any of string  say A we will go to ascii postion ie 65 and we will increment (while we are traversing ) and when this A come later in other string s2 then we will decrement. so carrying this for both the string we should get all elements of A[128] back to zero again . if we get any non zero value then it violate occurrence as stated above

Option a) and b are wrong because you are just checking first character of string there is no increment thing .So we cant say string are anagram by just checking charcter

option c ) is wrong too

beacuse in count [a[j++]|++ if currently i =o and then after exceuting i =1

and when it goes to B statament it check from 1 index of string and not from 0 index . we are leaving a charcter in each iteration

Option d ) Work perfectly fine . You first check character of string at index i , after doing that B increment so while loop can be excutued for other part of strings

NOTE : while (a[j] && b[j]]) is one that can be used to check anagarm string should be always of same length . If one is shorter or bigger the loop breaks and then they are not anagarm of each other :)
9 votes
9 votes

If you don't want to read all those long answers. Here the smaller way, the way I analyzed it.

  1. Anagram, as given in question "abc", is an anagram of "cba".

  2. Always try to run a complicated code with a very small example like above, you'll find out that if two string are permutations of each other than they make count[ ] array all 0 in last.

  3. we can also state the point 2 as the last line of the program want for( .... ) if(count [j]) return 0, i.e. if at any position other than 0  is found return with a message "Hey ! this is not an anagram."

 

options given:-

  • option c) A : count [a[j++]]++ and B : count[b[j]]--
  • option d) A : count [a[j]]++and B : count[b[j++]]--

 

  1. Then when you try to fill A & B, we are given j=0 intially but how to increment the j? you'll notice that j need to increased somehow this will eliminate option a) & b) as there is no 'j++' in them and we need to increase j to read full array A[ ] B[  ]. 

  2. the only option left is c) and d) now, as is option c) if j get increased in A(first line we need to fill) then we will mismatch the positions like as ( j=0 given before loop,see above in option c) A=count[0] and as post-increment happen B=count[1], but we need to increase j such that a[j] & b[j] have same value so only option d) remain which has 'j++' at correct position, because of which j will increase for next iteration of loop.

edited by
Answer:

Related questions

25 votes
25 votes
3 answers
1
Ishrat Jahan asked Nov 3, 2014
8,917 views
What is the output printed by the following program?#include <stdio.h int f(int n, int k) { if (n == 0) return 0; else if (n % 2) return f(n/2, 2*k) + k; else return f(n/...