Ques.
Write a function which prints the cards which are missing from a set of playing cards.Cards are represented in the form of string whose first letter represnts type and second is the card number.
S-1=one of pade
S-A=A's of spade
C-k=King of clubs
H=j=jack of Hearts....so on

Dani AI

Generated

Following and the replies from and , the reliable way to print missing cards is: generate a canonical 52-card set, normalize the input strings into the same canonical representation, insert the normalized inputs into a set, then iterate the canonical deck and print those not present. Normalization should strip non-alphanumeric characters, uppercase everything, and map alternative ten notations (for example T -> 10) so tokens with different formatting still match.

The implementation below is compact and practical: normalizeCard makes tokens comparable, missingCards builds the canonical deck and computes the set difference. This approach handles duplicates and malformed tokens by ignoring invalid normalizations (adjust validation to be stricter if needed). Complexity is O(m + 52) where m is number of input tokens.

#include <iostream>
#include <string>
#include <vector>
#include <unordered_set>
#include <algorithm>
#include <cctype>

using namespace std;

string normalizeCard(const string &s) {
    string t;
    for (char c : s) if (isalnum((unsigned char)c)) t.push_back(toupper((unsigned char)c));
    if (t.size() < 2) return "";
    char suit = t[0];
    string rank = t.substr(1);
    if (rank == "T") rank = "10"; // accept 'T' as ten
    return string(1, suit) + rank;
}

vector<string> missingCards(const vector<string>& input) {
    vector<string> suits = {"S","H","D","C"};
    vector<string> ranks = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
    unordered_set<string> have;
    for (auto &s : input) {
        string n = normalizeCard(s);
        if (!n.empty()) have.insert(n);
    }
    vector<string> miss;
    for (auto &su : suits) for (auto &ra : ranks) {
        string code = su + ra;
        if (!have.count(code)) miss.push_back(code);
    }
    return miss;
}

Notes: adapt normalizeCard if your input uses different suit letters or odd rank encodings. If input may contain invalid tokens, log them before ignoring so errors are visible.

Recommended Answers

All 3 Replies

At least attempt your homework before you ask for help.

I tried but couldnt find the solution so asked for help and I am not forcing anyone for help

At least attempt your homework before you ask for help.

I tried but couldnt find the solution

Haha, I think plgriffith meant: CREATE a solution, not google one.
Now on topic: how would you know which card was missing?

Niek

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.