Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For each indicate the value of my_length after executing each fragment of C code

ID: 3879638 • Letter: F

Question

For each indicate the value of my_length after executing each fragment of C code. Function strlen is a standard C library function. Specify N/A if the value of my_length cannot be determined.

Can anyone explain how this answers come from.(without using program.)

l) my_length is 7 char msg[20] "CPRE288"; = my-length = strlen(msg); ili) my_length is4 char msg[100] = {"c,,,P','R', 'E','','2','8', '8',''); int my-length strlen(msg); = iv) my_length is7 char msg[] = "iRobot CPRE288"; int my-length = strlen(msg + 7); v) my_length is char msg[] = "CPRE288"; int my-length = strlen(msg strlen(msg)); +

Explanation / Answer

ii)

     the standerd C function strlen() requires <string.h> header file

and it wil give or retun the length of string in integers. As we konw computers denote end of a string by ''

ie null character.ie it use the '' to denote the end of a string so it will give the number of characters upto the null character

Here

char msg[20] ="CPRE288"

ie   msg[0] = 'C'

      msg[1] = 'P'

      msg[2] = 'R'

      msg[3] = 'E'

      msg[4] = '2'

      msg[5] = '8'

      msg[6] = '8'

      msg[7] = ''

      msg[8] = 'e'    // may be anythig ie any garbage value

      msg[9] = 's'

      msg[10] = 'd'

          .....

          .....

So here it will return 7 because there are 7 values up to '' ie msg[0] - msg[6]

iii)

       Here also the answer is same because i have explained it in the above

ie the computer or the function strlen() will check for the first encounter of the '' , null character , it won't go after that So here it will give 4 ie C P R E

iv)

      In this case the length is 7 because

            we know that a string it self is an array , i mean that a string name is an array index

ie , When you are call the strlen () function , it has only one argument ie the name of the string

eg: strlen(msg)

             here it will check from msg[0] to up to find an ''   , null character ,

that means when you just specify the name of a string it will take the array at starting index

this is because when you take an string input from a user

ie    scanf("%s",name);

    you don't use scanf("%s",&name);    // wrong

           because string name itself an array

So here the solutin is ie , when you call strlen() with

     strlen(msg+1)

              then it will return or check from msg[1] to upto find a ''

here you called s

strlen(msg +7)

           that is it will check from msg[7] to ''

so the irobot will be skipped so the length is 7

v)

     I don't thing anything to say ,because we already discussed about it , ie here

we called strlen()   with parameter

strlen(msg + strlen(msg))

it will give 0   because

   first it will find inner strlen() funtion that will return 7 then the call will become

strlen(msg + 7)

this will check form msg[7 ] to null character that is end

so it will give 0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote