0

How do you make 2 array strings into 1 array string, where I can print out all the 52 playing cards?

my code:

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>


int main() {
    char deck[52];
    char suits[] = {"Hearts","Diamonds","Clubs","Spades"};
    char values[]= {"Ace","Two","Three","Four","Five","Six",\
                    "Seven","Eight","Nine","Ten","Jack",\
                    "Queen","King"};
    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);//prints out all the 52 playing cards
            d++;
        }
    }

    return 0;
}

When I executed the program, the problem comes up which asks me to debug the program or close the program, where I closed the program in the end, which returns nothing. Can you please give me the answer which works?

2
  • Did you write this code yourself? Commented Mar 13, 2015 at 5:44
  • yes I did. I'm just struggling how to understand C that's all Commented Mar 13, 2015 at 5:46

3 Answers 3

1

Check the below code which fixes the issues in your code: The problem with your code is you try to modify the actual string before printing and because of this there is a modified string in the next iteration. So just copy the values and suits to array and print it out as shown below.

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>


int main()
{
  int i=0;
  char deck[30] = "";
  char suits[][30] = {"Hearts","Diamonds","Clubs","Spades"};
  char values[][30]= {"Ace","Two","Three","Four","Five","Six",
                    "Seven","Eight","Nine","Ten","Jack",
                    "Queen","King"};
  int V, S;
  for ( S= 0; S <13; S++)
  {
    for (V =0; V< 4; V++){
    memset(deck,0,sizeof(deck));/* Clear the buffer before writing new value*/
    strcpy( deck, values[S]);
    strcat(deck,suits[V]);
    printf("%s\n", deck);//prints out all the 52 playing cards
    i++;
   }
  }
    printf("Number of playing cards: %d\n",i);

    return 0;
  }
Sign up to request clarification or add additional context in comments.

Comments

0

strcat() returns a char *, a pointer to a char, not a char.

You are not even required to even consider the return value of strcat() since the destination pointer (first argument) will now contain the concatenated string, assuming enough memory is already allocated.

So here in your code, you are trying to put the concatenated string to values[V] which could fail when memory already allocated to it becomes insufficient.

The best method would be to allocate some memory (as you did with deck[]) and set it all to zeroes. Then keep strcat()ing there.

strcat(deck, values[V]);
strcat(deck, suits[S]);

Comments

0

An alternative to using strcpy and strcat is to use sprintf.

#include<stdio.h>
#include<string.h>

#define NUM_SUITS 4
#define CARDS_PER_SUIT  13
#define TOTAL_CARDS (NUM_SUITS * CARDS_PER_SUIT)

int main() 
{
    char deck[TOTAL_CARDS][24];
    char* suits[NUM_SUITS] = {"Hearts","Diamonds","Clubs","Spades"};
    char* values[CARDS_PER_SUIT]= {"Ace","Two","Three","Four","Five","Six",
                    "Seven","Eight","Nine","Ten","Jack",
                    "Queen","King"};

    int s, c, i;

    for(s = 0; s < NUM_SUITS; s++)
    {
       for(c = 0; c < CARDS_PER_SUIT; c++)
       {
           sprintf(deck[(s * CARDS_PER_SUIT) + c], "%s of %s", values[c], suits[s]);
       }
    }

    for(i = 0; i < TOTAL_CARDS; i++)
    {
        printf("%s\n", deck[i]);
    }

    return 0;
}

2 Comments

how do you get - char deck[TOTAL_CARDS][24]?
24 characters is more than enough to fit any of the possible string combinations (e.g no combination is longer than "Queen of Diamonds") . It's an arbitrary number.

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.