C++ Programming - Objects and Classes - Discussion

Discussion Forum : Objects and Classes - Programs (Q.No. 7)
7.
What will be the output of the following program?
#include<iostream.h>
#include<string.h> 
class IndiaBix
{
    int val; 
    public:
    void SetValue(char *str1, char *str2)
    {
        val = strcspn(str1, str2);
    }
    void ShowValue()
    {
        cout<< val;
    } 
};
int main() 
{
    IndiaBix objBix;
    objBix.SetValue((char*)"India", (char*)"Bix"); 
    objBix.ShowValue(); 
    return 0; 
}
2
3
5
8
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
10 comments Page 1 of 1.

Samip Khatri said:   1 decade ago
The function strcspn() returns the index of the first character in str1 that matches any of the characters in str2.

Here "i" of str2 matches with the "i" of str1 which is 3rd character (starting from 0). So answer is 3.

Ashwak said:   1 decade ago
The function strcspn () compares the two strings for common character and returns the index of that character in the first string. It is case sensitive.

In this example the common character is 'i' which has the index '3' in the first string hence the answer.
(1)

Priya said:   1 decade ago
Tell about strcspn()?

Nishant sharma said:   10 years ago
strcspn() is a function that will tell about number of characters in the initial segment string which are not present in the second string.

In this case n, d, a of India are not present in the bix. Hence answer is 3.

Hassan maha said:   8 years ago
Thanks @Nishant.

Midhungopal said:   8 years ago
Can you explain more?

Garvit Jain said:   8 years ago
Scans str1 for the first occurrence of any of the characters that are part of str2, returning the number of characters of str1 read before this first occurrence.
str1
C string to be scanned.

str2
C string containing the characters to match.

Deepak nayak said:   7 years ago
It Scans str1 for the first occurrence of any of the characters that are part of str2, returning the number of characters of str1 read before this first occurrence.

Nathas said:   6 years ago
What happens if more than one character in str1 is common with str2?

In that case, how does this program output get change?will anyone explains me.

Raj said:   5 years ago
In str1, I present as 1st character then why 1 is not the correct answer?

Post your comments here:

Your comments will be displayed after verification.