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

String Homework 1. The following function calls supposedly write a single new-li

ID: 3771702 • Letter: S

Question

String Homework

1. The following function calls supposedly write a single new-line character, but some are incorrect. Identify which calls don’t work and explain why.

a) printf(“%c”,’ ’);

b) printf(“%c”, “ ”);

c) printf("%s", ' ');

d) printf("%s", " ");

e) printf(‘ ’);

f) printf(“ ”);

g) putchar(‘ ’);

h) putchar(“ ”);

i) puts(‘ ’);

j) puts(“ ”);

k) puts(“ “);

2. Suppose that p has been declared as follows:

char *p = "abc";

Which of the following function calls are legal? Show the output produced by each legal call, and explain why the others are illegal.

a) putchar(p);

b) putchar(*p);

c) puts(p);

d) puts(*p);

3. Suppose that str is an array of characters. Which of the following statements is no equivalent to the other three?

a) *str = 0;

b) str[0] = '';

c) strcpy(str, "");

d) strcat(str, "");

Explanation / Answer

1.
(b) printf("%c"," "); it will uses in "" and "" is used for string not for character

(c) printf("%s", ' '); it is printing ' ' as string and using %s for that but is ''

(e) printf(' ') always used "" for output but here ' ' is in printed place , that should be " "

(h) putchar(“ ”) always uses character so ' ' is right for that

(i) puts(' '); always used char string as argument and here ' ' is wrong

2.
(a) putchar(p); output : blank space
(b) putchar(*p); output : a
(c) puts(p); output : abc
(d) puts(*p) error because we are printing *p

3.d) strcat(str, "");