C++ Programming - Functions - Discussion
Discussion Forum : Functions - Programs (Q.No. 29)
29.
What will be the output of the following program?
#include<iostream.h>
#include<string.h>
class IndiaBix
{
char txtMsg[50];
public:
IndiaBix(char *str = NULL)
{
if(str != NULL)
strcpy(txtMsg, str);
}
int BixFunction(char ch);
};
int IndiaBix::BixFunction(char ch)
{
static int i = 0;
if(txtMsg[i++] == ch)
return strlen((txtMsg + i)) - i;
else
return BixFunction(ch);
}
int main()
{
IndiaBix objBix("Welcome to IndiaBix.com!");
cout<< objBix.BixFunction('t');
return 0;
}
Discussion:
7 comments Page 1 of 1.
K kushal said:
2 years ago
Here, it's asking strlen(txtmsg + i)-i
Means from o to end of the string's length - 9(i) = 6.
Means from o to end of the string's length - 9(i) = 6.
Ketaki said:
7 years ago
Here i is constant i=0;
Then in if (txtMsg[++i]).
value of i increases i=1;
but in if(txtMsg[++i]=='ch').
The condition not satisfied it goes to else part in else part we again call Bixfunction, Bixfunction call continuously until if condition satisfied if, if condition satisfied value of i is 9.
So i pointed to 9th position means character t.
in char o the length of string is 15,
So,txtMsg pointed 0th position means base address,
So,txtmsg+i means 0+9=9,
So txtMsg base address now points in 9 positions and length of the string is become 15 now and (txtMsg+i)-i means, 15-9=6.
The answer 6.
Then in if (txtMsg[++i]).
value of i increases i=1;
but in if(txtMsg[++i]=='ch').
The condition not satisfied it goes to else part in else part we again call Bixfunction, Bixfunction call continuously until if condition satisfied if, if condition satisfied value of i is 9.
So i pointed to 9th position means character t.
in char o the length of string is 15,
So,txtMsg pointed 0th position means base address,
So,txtmsg+i means 0+9=9,
So txtMsg base address now points in 9 positions and length of the string is become 15 now and (txtMsg+i)-i means, 15-9=6.
The answer 6.
Neha said:
7 years ago
Answer 6 is correct.
But 15+9-9 how is become 6? Please explain.
But 15+9-9 how is become 6? Please explain.
Amit Saxena said:
9 years ago
@Bhairavee.
Please check the expression it will do 24 + 9 - 9. So none of the option is correct.
Please check the expression it will do 24 + 9 - 9. So none of the option is correct.
Naina Gupta said:
9 years ago
Can anyone explain how answer is 6?
Bhairavee said:
1 decade ago
When the if condition becomes true, the value of i is 9. So strlen will give the length of the string from index 9(i.e.txtMsg+i) onwards.
Hence, length = total length-9 = 24-9 = 15.
Therefore, the value returned is 15-i = 15-9 = 6.
Hence, length = total length-9 = 24-9 = 15.
Therefore, the value returned is 15-i = 15-9 = 6.
Ash said:
1 decade ago
How the answer is 6?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers