d. The match function is supposed to return true if and only if its two C string
ID: 3747837 • Letter: D
Question
d. The match function is supposed to return true if and only if its two C string arguments have exactly same text. Explain what the problems with the implementation of the function are, and show a way to fix them. // return true if two C strings are equal bool match(const char stri, const char str21 ]) while (stri !. && str2 !-0) // zero bytes at ends if (stri != str2) // compare corresponding characters return false str1+i str2++ // advance to the next character return strstr2 both ended at same time? int main) char a[10] "pointy"; char bi1pointless if (match (a,b)) coutExplanation / Answer
Solution:
The solution you have provided in 2nd pic is wrong, the correct solution is below.
#include <iostream>
using namespace std;
// return true if two C strings are equal
bool match(const char str1[], const char str2[])
{
while (*str1 != 0 && *str2 != 0) // zero bytes at ends
{
if (*str1 != *str2) // compare corresponding characters
return false;
str1++; // advance to the next character
str2++;
}
return *str1 == *str2; // both ended at same time?
}
int main()
{
char a[10] = "pointy";
char b[10] = "pointless";
if (match(a,b))
cout << "They're the same! ";
else
cout<<"They are not same";
}
a and b are not the same string hence they should print They are not same. Where as your program is printing it as They are same.
Explanation:
* is called as the pointer.
When we do *str1 then we get p i.e the first letter of the word pointy.
When we do *str2 then we get p i.e the first letter of the word pointless.
have that basic understanding first.
Now, in the while loop we are checking if both the strings are not empty.
Now in the if statement we are checking each character of str1 with each character of str2 at the same position.
For example, initially *str1 = p and *str2 = p . if both are not same then we are returning false when we return false the main function print they are not same .
But whereas here *str1 =p and *str2 =p and both are same so control is not going into the if statement and then we are increamenting*str1 and *str2 to get next letter of their respective word. Now *str1 = o and *str2 = o hence again it won't go inside the if again it will increment to get the next letter, This process will go on till t. When it will increment to get the next letter after t, *str1 = y and *str2 = l now it will go inside the if statement because given statement inside the if condition is satisfied the match function will return false.
Now, suppose a and b both were same i.e point. Then it would have iterated till t and it would have been exited from while after while loop it returns a boolean expression i.e *str1==*str2 that means both are equal.
Note: I hope I have made you clear, if not please do comment below before giving any negative feedback. If you liked the explanation then don't forget to hit the like button.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.