63 839
Assignments Done
99,2%
Successfully Done
In August 2018

Answer to Question #76526 in C++ for LEE JIA WEI

Question #76526
Write a function num_dups that takes in a string and returns the number of duplicated characters in the string.

Hint: You can use a std::map to maintain a count of the characters.

#include <string>

using namespace std;

int num_dups(string str) {
//code

}
Expert's answer

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

Assignment Expert
28.04.18, 14:40



int main() {
cout << num_dups("mississippi") << endl;
return 0;
}

Assignment Expert
28.04.18, 14:40

{
if (!rep) num_of_dups++;
rep = true;
copy_str[comp_symbol] = '#';
}
}
}
return num_of_dups;
}

Assignment Expert
28.04.18, 14:40

string copy_str = str;
int num_of_dups = 0;
for (int symbol = 0; copy_str[symbol] != '\0'; symbol++)
{
bool rep = false;
for (int comp_symbol = symbol + 1; copy_str[comp_symbol] != '\0'; comp_symbol++)
{
if (copy_str[symbol] == copy_str[comp_symbol] && ( (copy_str[comp_symbol] >= 'A' &&
copy_str[comp_symbol] <= 'Z') || (copy_str[comp_symbol] >= 'a' && copy_str[comp_symbol] <= 'z') ))

Assignment Expert
28.04.18, 14:40

There was a misunderstanding what exactly should be calculated. Try this:

#include
using namespace std;

int num_dups(string str) {

LEE JIA WEI
27.04.18, 16:51

expected ans for num_dups("mississippi"); = 3
however get 7 for solution 1

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions