Knowee
Questions
Features
Study Tools

What will be the output of the following C code?const char str1[]="ABCDEF1234567";const char str2[] = "269";len = strcspn(str1, str2);printf("First matching character is at %d\n", len + 1);

Question

What will be the output of the following C code?const char str1[]="ABCDEF1234567";const char str2[] = "269";len = strcspn(str1, str2);printf("First matching character is at %d\n", len + 1);

🧐 Not the exact question you are looking for?Go ask a question

Solution

The output of the given C code will be "First matching character is at 11".

Here's the step by step explanation:

  1. The strcspn() function in C calculates the length of the initial segment of str1 which consists entirely of characters not in str2.

  2. In this case, str1 is "ABCDEF1234567" and str2 is "269".

  3. The function will start checking each character of str1 against all characters in str2.

  4. It will keep going until it finds a character in str1 that is also in str2.

  5. The first character in str1 that is also in str2 is '2', which is at the 11th position in str1 (remember, in C, the index starts from 0).

  6. Therefore, the strcspn() function will return 10.

  7. The printf statement then prints this value plus 1, so the output will be "First matching character is at 11".

This problem has been solved

Similar Questions

What is the output of the following code?char str[] = "hello";int len = strlen(str);printf("%d\n", len);

What is the output of the following code?char str1[] = "C programming";char str2[20];strcpy(str2, str1);printf("%s\n", str2);

What is the output of the following code?char str[] = "world";printf("%s\n", str + 2);

What will be the output of the following code?#include <stdio.h>int main() {    char str[] = "ABCDE";    printf("%c\n", str[2] + 3);    return 0;}

What is the output of the following code? 12345678#include <stdio.h> #include <string.h> int main() { char str[] = "Exploration of Mars"; char sub[] = "Mars"; printf("%s", strstr(str, sub)); return 0; }

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.