-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;
}
Edi G.
  • 2,432
  • 7
  • 24
  • 33
  • Sorry, I did not understand your problem, neither your code. Are they related? – Sourav Ghosh Mar 13 '15 at 07:35
  • `string = strcat( *values[V], *suits[S]);` wrong. – BLUEPIXY Mar 13 '15 at 07:36
  • @SouravGhosh http://stackoverflow.com/questions/29025599/how-to-make-two-array-strings-into-one-array-string-in-c/29025737#29025737 – Gopi Mar 13 '15 at 07:36
  • @Gopi yep, with the inclusion of some erroneous statement(s). :P – Sourav Ghosh Mar 13 '15 at 07:38
  • 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. – M Oehm Mar 13 '15 at 07: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. – Gopi Mar 13 '15 at 07:40
  • Strings are probably the worst possible way to represent playing cards in software. Computers use numbers: text is for humans. Make your cards simple integers and your code will be 1000x faster and 100x simpler. Convert to strings only when you need to talk to a human. – Lee Daniel Crocker Mar 13 '15 at 16:44

3 Answers3

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.
Karthikeyan.R.S
  • 3,991
  • 1
  • 19
  • 31
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;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
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.)

M Oehm
  • 28,726
  • 3
  • 31
  • 42