C library strcspn in C++ (legacy headers): for the given strings, what index does strcspn("India", "CuriousTab") return? #include #include class CuriousTab { int val; public: void SetValue(char str1, char str2) { val = strcspn(str1, str2); } void ShowValue() { cout << val; } }; int main() { CuriousTab objCuriousTab; objCuriousTab.SetValue((char)"India", (char)"CuriousTab"); objCuriousTab.ShowValue(); return 0; }

C++ Programming Objects and Classes Difficulty: Easy
Choose an option
  • A
    2
  • B
    3
  • C
    5
  • D
    8
  • E
    It returns -1 when there is a match.

Answer

Correct Answer: 3

Explanation

Introduction / Context:strcspn returns the length of the initial segment of the first string that consists of characters not found in the second string. Once a character from the second string appears in the first string, the count stops, and that index is returned.

Given Data / Assumptions:

  • First string: "India".
  • Second string: "CuriousTab" (case-sensitive).
  • We compute strcspn("India", "CuriousTab").

Concept / Approach:Scan "India" left to right until a character appears that is present in "CuriousTab". The function returns the count of characters before that first match.

Step-by-Step Solution:Check characters in order: I (uppercase) not in "CuriousTab".n not in the second string.d not in the second string.i (lowercase) is in "CuriousTab".Therefore the initial non-matching span length is 3, so the function returns 3.

Verification / Alternative check:Print strcspn outputs for intermediate prefixes to confirm behavior; also note the function is case-sensitive.

Why Other Options Are Wrong:2 stops too early; 5 or 8 ignore the early match; returning -1 is not how strcspn signals matches.

Common Pitfalls:Confusing strcspn with strspn, or forgetting its case sensitivity.

Final Answer:3

Discussion & Comments
No comments yet. Be the first to comment!
Join Discussion