-2

I wanted to return a specific array string from a deck of 52 playing cards, where this string array 'deck[]' has combined string arrays: suits and values. How would you do this and will you show me the code on how you do this?

My code:

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
/* handy typedefs */
typedef unsigned char card;
typedef unsigned char pairs;

/* arrays for the names of things */
 static char *suits[] = {"Hearts","Diamonds","Clubs","Spades"};
 static char *values[]= {"Ace","Two","Three","Four","Five","Six",\
                    "Seven","Eight","Nine","Ten","Jack",\
                    "Queen","King"};

int main()
{
    card deck[52];

    int V, S, d = 0;
    char string;
    for ( S= 0; S <4; S++)
    for (V =0; V< 13; V++){
        string = strcat( *values[V], *suits[S]);
        deck[d] = string;
        printf("%s\n", string);
        d++;
    }
    printf("%s\n", deck[2]);
    return 0;
}
7
  • Sorry, I did not understand your problem, neither your code. Are they related? Commented Mar 13, 2015 at 7:35
  • string = strcat( *values[V], *suits[S]); wrong. Commented Mar 13, 2015 at 7:36
  • @SouravGhosh stackoverflow.com/questions/29025599/… Commented Mar 13, 2015 at 7:36
  • 2
    Strings are somewhat complicated in C and they are nor a good representation of a deck of cards. (And your "handy" typedefs are anything but.) A better approach might be to use enums for suits and values, to combine them into a card struct and to write a function to print the name of a card. Commented Mar 13, 2015 at 7:40
  • 1
    What are you trying to do? deck[d] is wrong and I already pointed out this in your previous question and you continue to use the same. Commented Mar 13, 2015 at 7:40

3 Answers 3

0

In your code lot of things to correct,

First one typedef of card. In main function you are using that as a array of pointers but you are defined as a character. So change that into like this.

typedef unsigned char *card;

In strcat, you are doing that with the character. If you remove that * then it will lead to accessing the read only memory. For all that try this line in inner loop. And remove your strcpy.

Declare the string as like this char *string;

string=malloc(50); // 50 is a bytes use what you need.
strcpy(string,values[V]);//first copy the values 
strcat(string,suits[S]);// Then their corresponding suites.
Sign up to request clarification or add additional context in comments.

Comments

0

You asked how to concatenate two strings. You can use strcat for this, but you must provide memory for it. You can allocate memory with malloc, which means that you have to free it later. Or you can make the cards char arrays of a fixed size, which means that you have to copy around contents of char buffers with strcpy when you exchange cards in your deck. Later, when you want to determine hands or compare suits and values, you have to compare partial strings, which is complicated.

So this is not an answer to your question, but a suggestion for a better approach to representing cards. I think strings are not a good representation of cards in a deck and therefore your question is headed in the wrong direction in the first place.

Consider making a card a struct of two enumerated values, the card's suit and rank:

enum Suit {
    Hearts, Diamonds, Clubs, Spades, NSuit
};

enum Rank {
    Ace, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten,
    Jack, King, Queen, NRank
};

#define NCard (NSuit * NRank)

struct Card {
    enum Suit suit;
    enum Rank rank;
};

Now it is easy to compare suits and ranks of cards or to sort them by value or to tell whether they are face cards.

There are several ways to print cards. I suggest a solution that fills a char buffer of a given maximum size:

static char *suit_name[] = { "Hearts", "Diamonds", "Clubs", "Spades" };
static char *rank_name[] = { "Ace", "Two", "Three", "Four", "Five", "Six",
    "Seven", "Eight", "Nine", "Ten", "Jack","Queen", "King"
};

int cardstr(char *buf, size_t n, struct Card card)
{
    return snprintf(buf, n, "%s of %s",
        rank_name[card.rank], suit_name[card.suit]);
}

You can then pass that string to the %s format of printf later:

int main()
{
    struct Card card[NCard];

    int d = 0;

    // Create deck in factory order
    for (int s = 0; s < NSuit; s++) {
        for (int r = 0; r < NRank; r++) {
            card[d].suit = s;
            card[d].rank = r;
            d++;
        }
    }

    // Print cards        
    for (d = 0; d < NCard; d++) {
        char buf[20];

        cardstr(buf, sizeof(buf), card[d]);
        printf("%s\n", buf);
    }

    return 0;
}

(This is very similar to Bluepixie's answer, only much more long-winded.)

Comments

0

I think that there is no need to synthesize pre string for display.

#define DISPLAY(card) printf("%s%s\n", values[card % 16], suits[card / 16])

int main(void){
    card deck[52];

    int V, S, d = 0;
    for ( S= 0; S <4; S++)
        for (V =0; V< 13; V++){
            deck[d++] = S * 16 + V;
        }
    DISPLAY(deck[2]);
    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.