Question: string winner ( string erica, string bob ) { int ericaScore = 0 ; / / Initialize Erica's score to 0 int bobScore = 0

string winner(string erica, string bob){
int ericaScore =0; // Initialize Erica's score to 0
int bobScore =0; // Initialize Bob's score to 0
int n = erica.length(); // Get the length of the input strings
// Loop through each day's problem
for (int i =0; i < n; i++){
// Check the difficulty of the problem Erica solved on that day
if (erica[i]=='E'){
ericaScore +=1; // Add 1 point for an easy problem
} else if (erica[i]=='M'){
ericaScore +=3; // Add 3 points for a medium problem
} else if (erica[i]=='H'){
ericaScore +=5; // Add 5 points for a hard problem
}
// Repeat the same steps for the problem Bob solved on that day
if (bob[i]=='E'){
bobScore +=1;
} else if (bob[i]=='M'){
bobScore +=3;
} else if (bob[i]=='H'){
bobScore +=5;
}
}
// Compare scores to determine the winner
if (ericaScore > bobScore){
return "Erica"; // Erica wins
} else if (bobScore > ericaScore){
return "Bob"; // Bob wins
} else {
return "Tie"; // It's a tie
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!