retagged by
364 views
3 votes
3 votes
#include<stdio.h>
#include<string.h>
char upstr[50];

void putStar (int n,char str[]){
    upstr[n] = str[n];
    if(n == strlen(str))
        return;
    else upstr[n+1] = '*';
    putStar(n+1,str);
}

int main()
{
    char str[] = "hello";
    putStar (0, str);
    printf("%s", upstr); 
}

What will be the output of the given program?

  1. h*e*l*l*o
  2. h*e*l*l*o*
  3. h*e*l*l*
  4. hello
retagged by

1 Answer

5 votes
5 votes

we’ll call putStar( , ) as PS( , )

str[] = “hello”; strlen(str) = 5

  1. PS(0,str) → upStar[0] = str[0] = ‘h’; 0 != 5 thus upStar[1] = ‘*’; nextcall
  2. PS(1,str) → upStar[1] = str[1] = ‘e’; 1 != 5 thus upStar[2] = ‘*’; nextcall
  3. PS(2,str) → upStar[2] = str[2] = ‘l’; 2 != 5 thus upStar[3] = ‘*’; nextcall
  4. PS(3,str) → upStar[3] = str[3] = ‘l’; 3 != 5 thus upStar[4] = ‘*’; nextcall
  5. PS(4,str) → upStar[4] = str[4] = ‘o’; 4 != 5 thus upStar[5] = ‘*’; nextcall
  6. PS(5,str) → upStar[5] = str[5] = ‘\0’; 5 == 5 thus return;

Thus, upStar[] contains “hello\0”. Notice, every ‘*’ is again overwritten by corresponding char from str[].

Answer :- D. hello

Answer:

Related questions

5 votes
5 votes
1 answer
1