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);
Solution
The output of the given C code will be "First matching character is at 11".
Here's the step by step explanation:
-
The
strcspn()function in C calculates the length of the initial segment ofstr1which consists entirely of characters not instr2. -
In this case,
str1is "ABCDEF1234567" andstr2is "269". -
The function will start checking each character of
str1against all characters instr2. -
It will keep going until it finds a character in
str1that is also instr2. -
The first character in
str1that is also instr2is '2', which is at the 11th position instr1(remember, in C, the index starts from 0). -
Therefore, the
strcspn()function will return 10. -
The
printfstatement then prints this value plus 1, so the output will be "First matching character is at 11".
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; }
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.