简体   繁体   中英

how to make two array strings into one array string in C

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?

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;
  }

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]);

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM