• edited by
15,931 views
47 47 votes

Consider the following C program:

void abc(char*s)
{
    if(s[0]=='\0')return;
    abc(s+1);
    abc(s+1);
    printf("%c",s[0]);
}

main()
{ 
    abc("123");
}
  1. What will be the output of the program?
  2. If $abc(s)$ is called with a null-terminated string $s$ of length $n$ characters (not counting the null ('\0') character), how many characters will be printed by $abc(s)$?

3 Answers

Best answer
57 57 votes

Answer (A) $:332 \ 332 \ 1$
Answer (B) $:2^n-1$

Q. (A) $O/p$ :

$3 3 2 3 3 2 1$

(B) : $T(n) = 2T(n-1) +1 ; n>0 $

$\qquad =  0 ; n=0$ [Since for length zero string no character will be printed]

After solving it by substitution,

$T(n) = 2T(n-1) +1$

$\qquad = 2(2T(n-2)  + 1 )  +1$

$\qquad = 2^2T(n-2)  +  2 +1$

$\qquad =  2^2(2T(n-3) +1 )  + 2 + 1$

$\qquad = 2^3T(n-3) + 2^2  + 2 + 1$

Finally, it will expand like

$T(n)= 2^nT(n-n) + 2^{n-1} + 2^{n-2} + \ldots + 2^2 + 2 + 1 $

$\qquad = 2^nT(0) + 2^{n-1} + 2^{n-2} + \ldots+ 2^2 + 2 + 1$

$\qquad = \frac{1.(2^n -1)}{(2 - 1)}$

$\qquad = 2^n -1$

• edited by
6 6 votes
ans for B is: 2^n -1

for length 2 -> 3 characters

for length 3-> 7 characters

for length 4-> 15 characters

hence for n-> 2^n - 1 characters
Position:
Show:

Related questions

17 17 votes
5 answers 5 answers
10.3k
10.3k views
Kathleen asked Sep 14, 2014
10,265 views
Consider the following programProgram P2 var n : int; procedure W(var x : int) begin x = x + 1; print x; end procedure D begin var n : int; n = 3; W(n); end begin \\begin...
99 99 votes
4 answers 4 answers
40.6k
40.6k views
Kathleen asked Sep 14, 2014
40,569 views
Consider the following three C functions:$[P1]$ int *g(void) { int x = 10; return (&x); }$[P2]$ int *g(void) { int *px; *px = 10; return px; }$[P3]$ int *g(void) { int *p...
13 13 votes
4 answers 4 answers
4.4k
4.4k views
go_editor asked Feb 8, 2018
4,388 views
Consider a relation examinee (regno, name, score), where regno is the primary key to score is a real number.Write an SQL query to list the regno of examinees who have a s...
12 12 votes
3 3 answers
4.5k
4.5k views
go_editor asked Feb 8, 2018
4,450 views
Consider a relation $\text{examinee (regno, name, score)},$ where regno is the primary key to score is a real number.Suppose the relation $\text{appears (regno, centr_cod...