47.6k views
1 vote
Let f be the following function: int f(char *s, char *t){char *p1, *p2;for(p1 = s, p2 = t; *p1 != ‘\0’&& *p2 != ‘\0’; p1++, p2++){if (*p1 == *p2) break; }return p1 –s;}What is the return value of f(“abcd”, “bccd”)?

User JimiLoe
by
6.1k points

1 Answer

1 vote

Answer:

return value =2.

Here the function f() returns the length of the substring we traversed before we find the same character at the equal index of two substrings.

Take the inputs s= “abcd” and t= “bccd”.

• Now, p1 points to s1, i.e., p1 points to the character ‘a’ of “abcd”. And similarly, p2 points to ‘b’ of “bccd”.

• Then we compare the values at p1 and p2, are not equal, so p1 and p2 both are incremented by 1.

• Now the characters ‘b’ and ‘c’ of “abcd” and “bccd” respectively are compared. They are not equal. So both p1 and p2 both are incremented by 1.

• Now, p1 points to ‘c’ of “ abcd” that is the element at index 2 of s. And p2 points to ‘c’ of “bccd” that is the element at index 2 of t. Here value at p1 and p2 becomes equal. So the break statement is executed. We stop moving forward.

• As p1 is pointing to index 2 and s is pointing to the base that is index 0, so p1-s = 2.

Step-by-step explanation:

#include<stdio.h>

int f(char *s, char *t);

void main()

{

int k = f("abcd", "bccd");

printf("%d", k);

}

int f(char *s, char *t)

{

char *p1, *p2;

for(p1 = s, p2 = t; *p1 != '\0'&& *p2 != '\0'; p1++, p2++)

{

if (*p1 ==*p2)

break;

}

return (p1-s);

}

OUPUT is given as image

Let f be the following function: int f(char *s, char *t){char *p1, *p2;for(p1 = s-example-1
User Silvaric
by
5.4k points