Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

CODE in C++: Write a function that compares two strings of equal length in one o

ID: 3852998 • Letter: C

Question

CODE in C++:

Write a function that compares two strings of equal length in one of two ways. Both comparison methods compare the characters of one string to the characters of the other string in the location (e.g., the first letter of the first string to the first letter of the second string, et cetera). The two comparison methods are: "same": Calculates and returns a percentage of how similar two strings are "diff": Calculates and returns a percentage of how dissimilar two strings are If the function is called without either of those strings as the comparison method, your function should return 0. Use the following function header: double stringComparator(string stringOne, string stringTwo, string comparisonType) For example: Calling your function and using the values ("Computer", "confuser", "diff"), you would return 5 because 4 out of 8 letters are different. Calling your function and using the values ("pickle", "sticks", "same") would return 0 since no letters are the same. Calling your function and using the values ("computer", "computer", "difference") would return 0 since the comparison Type value is invalid.

Explanation / Answer

Here is the C++ code for the question with output.

Please rate the answer if it helped. Thank you.

#include <iostream>

using namespace std;

double stringComparator(string stringOne, string stringTwo, string comparisonType)

{

int count = 0;

if(comparisonType == "same")

{

  

for(int i = 0; i < stringOne.length(); i++)

{

if(stringOne[i] == stringTwo[i]) //increment count when there is match

count++;

  

}

}

else if(comparisonType == "diff")

{

  

for(int i = 0; i < stringOne.length(); i++)

{

if(stringOne[i] != stringTwo[i]) //increment count when there is a mismatch

count++;

  

}

}

  

return count * 1.0 / stringOne.length();

}

int main()

{

cout << "calling stringComparator("Computer", "confuser", "diff") = " << stringComparator("Computer", "confuser", "diff") << endl;

cout << "calling stringComparator("pickle", "sticks", "same") = " << stringComparator("pickle", "sticks", "same") << endl;

cout << "calling stringComparator("computer", "computer", "difference") = " << stringComparator("computer", "computer", "difference") << endl;

  

}

output

calling stringComparator("Computer", "confuser", "diff") = 0.5
calling stringComparator("pickle", "sticks", "same") = 0
calling stringComparator("computer", "computer", "difference") = 0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote