Home » Programming & Data Structure » Programming and data structure miscellaneous » Question

Programming and data structure miscellaneous

Programming & Data Structure

  1. Consider the following C program segment :
       char p [20]
        char *s = “string”;
        int length = strlen (s);
        for (i = 0; i < length, i++)
        p[i] = s [length – i];
        print f(“%s”, p);
    The output of the program is
    1. gnirts
    2. garbage dashing or no value printed
    3. gnirt
    4. no output is printed
Correct Option: D

Let us consider below line inside the for loop.
P[i] = S[length – i];
For i = 0, P[i] will be S[6 – 0) and S [6] is ‘10’.
So, P[0] becomes ‘10’. It does not matter what comes in P[1], P[2] ........ as P[0] will not change for i > 0.
Nothing is Printed if we print a string with first character ‘10’.



Your comments will be displayed only after manual approval.