Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Suppose youre tasked with fixing a function definition that does not work as intended. The function is supposed to compare two strings and set the

Suppose you’re tasked with fixing a function definition that does not work as intended. The function is supposed to compare two strings and set the count to the number of identical characters, two characters are identical if they are the same character and are in the same position in the cstring. Note that cstrings are just character arrays that have ‘\0’ as their last character, for example

char name[7] = "harry";

might looks like this in memory:

h a r r y \0

The function usage is as follows:

compareCstrings(“SMC”, “SBCC”, count); // should set count to 1 compareCstrings(“basketball”, “Baseball”, count); // should set count to 2

Currently the function definition is:

void compareCstrings(const char *str1, const char *str2, int &count) { *count = 0; while (str1 != ‘\0’ || str2 != ‘\0’) { if( *str1 == *str2) *count++; str1++; str2++; } }

But this does not work, determine why not?

Step by Step Solution

3.46 Rating (156 Votes )

There are 3 Steps involved in it

Step: 1

Issue Issue in your code is that you are comparing the entire string n... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Statistics For Engineers And Scientists

Authors: William Navidi

4th Edition

73401331, 978-0073401331

More Books

Students also viewed these Programming questions

Question

=+a. Calculate the mean and standard deviation of x.

Answered: 1 week ago