count duplicate letters

Oct 2, 2019 at 5:47pm
how do i count the dupes
for example
Input name1: james
Input name2: james
so total is 10 dupes because it counts not detect
detect means 5 i need the how to count the dupes
how does one get or count the duplicated letters?

Last edited on Oct 2, 2019 at 5:48pm
Oct 2, 2019 at 5:52pm
slow down and try english.
what the heck do you want to do, exactly?
what is the output for aaaa bbbb for the 2 strings? For aaaa abbbb ? Does 'a' == 'A' ?

most likely you want some to count with tables, but we need the problem description in full.
if you don't care about performance you can sort both strings up front and then muddle through them that way.
Last edited on Oct 2, 2019 at 5:54pm
Oct 2, 2019 at 6:01pm
really need this for my code
so basically if i input "james" name1
and still "james" on name 2
it counts all the duplicated letters
input1 | input2
J A M E S | J A M E S
1 2 3 4 5 | 6 7 8 9 10
Last edited on Oct 2, 2019 at 6:01pm
Oct 2, 2019 at 6:30pm
Okay, what have you attempted to do in C++ code?

1
2
3
4
5
dup count = 0
n = min(input1.length(), input2.length())
for i in range [0..n)
    if input1[i] == input2[i]
        increment dup count

I still don't get why that means 10 duplicates. Perhaps you should give us another examples besides "james james".
Last edited on Oct 2, 2019 at 6:33pm
Oct 2, 2019 at 6:44pm
im just gonna give you the whole code its a flames proj
#include<iostream>
#include<string>
#include<cctype>
#include<cstring>
using namespace std;
int main()
{
int points, ans;
string nme1;
string nme2;
system("cls");
cout<<"Input Your Fullname: ";
getline(cin,nme1);
cout<<"Input Your Partners Fullname: ";
getline(cin,nme2);
points=0;
if(nme1.length()<=2)
{
cout<<"\nPlease Enter a name and try again.";
}
else
{
for (size_t x = 0; x < nme1.length(); x++)
{
nme1[x] = tolower(nme1[x]);
for (size_t y = 0; y < nme2.length(); y++)
{
nme2[y] = tolower(nme2[y]);
if(nme1[x]==nme2[y])
{
points++;
break;
}
else if(points!=0)
{
points++;
break;
}
for(size_t n = 0; n < nme1.length();n++)
{
if(nme1[x]==nme2[n])
{
points++;
break; }}}}
points=points*2;
cout<<"\nRESULTS:";
switch(points%6)
{
case 0:
cout<<"\n\nWALANG FOREVER, WALANG KAMI.....\n";
break;
case 1:
cout<<"\n\n"<<nme1<<" AND "<<nme2<<" are Friends only. Friendzone.....";
break;
case 2:
cout<<"\n\n"<<nme1<<" and "<<nme2<<" are LOVERS, Uwian na besh, may NANALO na besh.\n";
break;
case 3:
cout<<"\n\nACQUAINTANCE ( MURAG NON-EXISTENT RAKA PARA NIYA! )\n";
break;
case 4:
cout<<"\n\n"<<nme1<<" and "<<nme2<<" are goint to MARRY one another.\n";
break;
case 5:
cout<<"\n\n"<<nme1<<" and "<<nme2<<" are ENEMIES....\n";
break;
}
}
return 0;
}


also to answer the 10 duplicates
here another example
INPUT1 : "butt"man
INPUT2 : "buttb"oyl
so basically the duplicated letters are butt + b ignore the " so overall count the "butt" on input 1 and "buttb" on input 2 total is : 9
input1............input2
b u t t............b u t t b
1 2 3 4..........5 6 7 8 9
ignore the dots in the middle

Oct 4, 2019 at 7:38am
Please use the source code formatter...

ALSO, lmao @ the switch output #lamporeber.
Oct 4, 2019 at 12:06pm
jonnin wrote:
most likely you want some to count with tables ... you can sort both strings

On jonnin’s advice, here’s an example about how to collect your data into tables you can later take advantage of (assumes character order is NOT important):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <cctype>
#include <iostream>
#include <map>
#include <string>


int main()
{
    std::string nme1;
    while( nme1.length() < 3 ) {
        std::cout << "Input your fullname: ";
        std::getline(std::cin, nme1);
        if( nme1.length() < 3 ) {
            std::cout << "\nPlease enter a name and try again.";
        }
    }
    std::map<char, int> table1;
    for(auto c : nme1) {
        if( std::isalpha( c ) ) {
            ++table1[static_cast<char>( ::tolower( c ) )];
        }
    }

    std::string nme2;
    while( nme2.length() < 3 ) {
        std::cout << "Input your partner's fullname: ";
        std::getline(std::cin, nme2);
        if( nme2.length() < 3 ) {
            std::cout << "\nPlease enter a name and try again.";
        }
    }
    std::map<char, int> table2;
    for(auto c : nme2) {
        if( std::isalpha( c ) ) {
            ++table2[static_cast<char>( ::tolower( c ) )];
        }
    }

    // Now you have two ordered tables with the main data - just use them.
    // For example:
    // Do you want to sum all the occurences of a character c in "nme1" to all
    // the occurencies of the same character in "nme2" ?
    int how_many {};
    for(const auto& [c, i] : table1 ) {
        // TODO: delete this after debug:
        std::cerr << "instances of " << c << " --> in nme1: " << i
                  << " in nme2: ";

        try {
            how_many += i + table2.at(c);
            // TODO: delete this after debug:
            std::cerr << table2.at(c) << '\n';
        } catch (std::out_of_range&) {
        // TODO: delete this after debug:
            std::cerr << "0\n";
            /**/
        }
    }

    // TODO: delete this after debug:
    std::cerr << "Total common characters: " << how_many << '\n';

    std::cout << "\nRESULTS:\n\n";
    switch(how_many % 6) {
    case 0:
        std::cout << "WALANG FOREVER, WALANG KAMI...\n";
        break;
    case 1:
        std::cout << nme1 << " and " << nme2 << " are Friends only. Friendzone...";
        break;
    case 2:
        std::cout << nme1 << " and " << nme2 << " are LOVERS, Uwian na besh, "
                                                "may NANALO na besh.\n";
        break;
    case 3:
        std::cout << "ACQUAINTANCE ( MURAG NON-EXISTENT RAKA PARA NIYA! )\n";
        break;
    case 4:
        std::cout << nme1 << " and " << nme2 << " are goint to MARRY one another.\n";
        break;
    case 5:
        std::cout << nme1 << " and " << nme2 << " are ENEMIES....\n";
        break;
    }
    return 0;
}


Output:
Input your fullname: "butt"man
Input your partner's fullname: "buttb"oyl
instances of a --> in nme1: 1 in nme2: 0
instances of b --> in nme1: 1 in nme2: 2
instances of m --> in nme1: 1 in nme2: 0
instances of n --> in nme1: 1 in nme2: 0
instances of t --> in nme1: 2 in nme2: 2
instances of u --> in nme1: 1 in nme2: 1
Total common characters: 9

RESULTS:

ACQUAINTANCE ( MURAG NON-EXISTENT RAKA PARA NIYA! )

Topic archived. No new replies allowed.