retagged by
455 views

1 Answer

0 votes
0 votes

//Here, I am assuming you know the logic of the program.
//Drop a comment if you want explanation regarding program's working.
//
//Also assuming that you have the basic understanding of:
//     && operator
//     while loop
//       post|pre decrement|increment
//



Basics:

"DBC" is stored as: D B C \0  // \0 means 0 (acts as string terminator.)
"BD"  is stored as: B D \0    


Code Fragment of Interest:

0        j = 0;
1        while (a[j] && b[j]) {
2        count [a[j]]++;
3        count[b[j++]]--;
4        }
5        for (j = 0; j < 128; j++)
6        {
7        if (count [j])
8            return 0;
9        }
10          return 1;


Stepping through the code fragment:

--Line 0 to Line 4---------------------------------------------

-------
j = 0 |
-------

Line 1:
    a[j] = a[0] = 'D' = 68 //ASCII representation of 'D'
    b[j] = b[0] = 'B' = 66 //ASCII representation of 'B'
    
    a[j]&&b[j] = 68 && 66 = 1

    Therefore, while (a[j] && b[j]) = while (1)

    Hence, code jumps to line 2.
Line 2:
        
    count[a[j]]++ = count[a[0]]++ = count[68]++;

    Now count array is something like this:

              0 0 0 0 ............ 1.............0   0
             ^ ^ ^ ^            ^           ^    ^
              |   |  |   |              |             |     |
  index   0 1  2 3            68          126 127
    
Line 3:
        
    count[b[j++]]-- = count[b[0]]-- = count[66]--;
    //As a sideeffect, j has also been incremented to 1.

    Now count array is something like this:

              0 0 0 0 .........0 -1  0  1.............0   0
             ^ ^ ^ ^           ^  ^  ^             ^   ^
              |  |   |   |              |   |   |               |     |
  index   0 1  2 3            66 67 68          126  127
    
    
-------
j = 1 |
-------

Line 1:
    a[j] = a[1] = 'B' = 66
    b[j] = b[1] = 'D' = 68
    
    a[j]&&b[j] = 66 && 68 = 1

    Therefore, while (a[j] && b[j]) = while (1)

    Hence, code jumps to line 2.
Line 2:
        
    count[a[j]]++ = count[a[1]]++ = count[66]++;

    Now count array is something like this:


              0 0 0 0 .........0  0  0  1.............0   0
             ^ ^ ^ ^           ^  ^  ^             ^   ^
              |  |   |   |              |   |   |               |     |
  index   0 1  2 3            66 67 68          126  127    


Line 3:
        
    count[b[j++]]-- = count[b[1]]-- = count[68]--;
    //As a sideeffect, j has also been incremented to 2.

    Now count array is something like this:

              0 0 0 0 .........0  0  0  0.............0   0
             ^ ^ ^ ^           ^  ^  ^             ^   ^
              |  |   |   |              |   |   |               |     |
  index   0 1  2 3            66 67 68          126  127
    
-------
j = 2 |
-------

Line 1:
    a[j] = a[1] = 'C'    = 67
    b[j] = b[1] = '\000' = 0
    
    a[j]&&b[j] = 67 && 0 = 0

    Therefore, while (a[j] && b[j]) = while (0)

    Hence, code jumps out of while loop.


--Line 5 to Line 9---------------------------------------------

Execution continues from line 5.

    Since, count array is something like this now:

              0 0 0 0 .........0  0  0  0.............0   0 (all zeroes)
             ^ ^ ^ ^           ^  ^  ^             ^   ^
              |  |   |   |              |   |   |               |     |
  index   0 1  2 3            66 67 68          126  127

    Therefore, if condition on line 7, never gets executed.

    Finally, code jumps out of for loop.

--Line 10--------------------------------------------------------

Now, return statement on line 10 is executed.

    Hence, anagram("DBC", "BD") returns 1.    
----------------------------------------------------------------

Related questions

2 votes
2 votes
1 answer
3
Çșȇ ʛấẗẻ asked Aug 28, 2016
380 views
Consider the following code:int P=0; for (i=1; i<2n; i++) { for (j=1; j<=n; j++) { if (j<i) P =P+1; } } printf(“%d”, P);What is the output printed by the above code i...
25 votes
25 votes
3 answers
4
Ishrat Jahan asked Nov 3, 2014
8,916 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/...