簡體   English   中英

如何在C編程中將兩個字符串數組放在一起

[英]How to put together two string arrays in C Programming

我有這個項目。 我有分數,match1,match2的數組,我有球員的名字和他們國家的縮寫。 然后我有這些縮寫和這個國家的簡稱。

我可以將球員名稱和國家/地區縮寫合並在一起,也可以將國家/地區縮寫和國家/地區簡稱合並在一起。

但我不知道如何將國家/地區的長名稱和球員名稱組合在一起。

因此,我創建了一個數組(char name_and_country [PLAYERS] [LENGTH_NAME]),我希望在此數組中使用名稱和國家/地區; 所以如果我打印說:

char name_and_country [PLAYERS] [LENGTH_NAME] = {“ David Beckham England”,“ Wayne Rooney England” ....等等。

有人可以幫我嗎? 先感謝您!

#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#define PLAYERS 5
#define NUM_COUNTRIES 3
#define LENGTH_NAME 40
#define LENGTH_CODE 4
#define LENGTH_COUNTRY 20


int main (void)

{   int match1[PLAYERS] = { 0,1,3,2,4};
    int match2[PLAYERS] = { 0,4,0,0,1};
    int goals[PLAYERS] ;

    char name[PLAYERS][LENGTH_NAME] ={"David Beckham","Wayne Rooney","Pirlo", "Del Piero","Lionel Messi"};
    char country_abbreviations[PLAYERS][LENGTH_CODE] = {"ENG","ENG","ITA","ITA","ARG"};
    char country_code[NUM_COUNTRIES][LENGTH_CODE] = {"ARG","ENG","ITA"};
    char country_name[NUM_COUNTRIES][LENGTH_COUNTRY] = {"Argentina", "England","Italy"};
    char name_and_country_code[PLAYERS][LENGTH_NAME];
    char country_code_and_country_name[NUM_COUNTRIES][LENGTH_COUNTRY];
    char name_and_country[PLAYERS][LENGTH_NAME];
    int i, first =1, second= 2;

 for(i=0; i < PLAYERS; i++)
    {
        strcpy (name_and_country_code[i], name[i]);
        strcat (name_and_country_code[i], "  " );
        strcat (name_and_country_code[i], country_abbreviations[i]);
        goals[i]= match1[i] + match2[i];
        printf("Player %s----- score %d:\n", name_and_country_code[i], goals[i]);
    }

使用此代碼。 我已將代碼添加到您的代碼中,結果如下。 我運行了,它工作正常。

#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include<string.h>
#define PLAYERS 5

#define NUM_COUNTRIES 3
#define LENGTH_NAME 40
#define LENGTH_CODE 4
#define LENGTH_COUNTRY 20


int main (void)

{   int match1[PLAYERS] = { 0,1,3,2,4};
        int match2[PLAYERS] = { 0,4,0,0,1};
        int goals[PLAYERS] ;

        char name[PLAYERS][LENGTH_NAME] ={"David Beckham","Wayne Rooney","Pirlo", "Del Piero","Lionel Messi"};
        char country_abbreviations[PLAYERS][LENGTH_CODE] = {"ENG","ENG","ITA","ITA","ARG"};
        char country_code[NUM_COUNTRIES][LENGTH_CODE] = {"ARG","ENG","ITA"};
        char country_name[NUM_COUNTRIES][LENGTH_COUNTRY] = {"Argentina", "England","Italy"};
        char name_and_country_code[PLAYERS][LENGTH_NAME];
        char country_code_and_country_name[NUM_COUNTRIES][LENGTH_COUNTRY];
        char name_and_country[PLAYERS][LENGTH_NAME];
        int i, first =1, second= 2;

        for(i=0; i < PLAYERS; i++)
        {
                strcpy (name_and_country_code[i], name[i]);
                strcat (name_and_country_code[i], "  " );
                strcat (name_and_country_code[i], country_abbreviations[i]);
                goals[i]= match1[i] + match2[i];
                printf("Player %s----- score %d:\n", name_and_country_code[i], goals[i]);
        }
        for(i=0; i < PLAYERS; i++)
        {
                strcpy (name_and_country[i], name[i]);
                strcat (name_and_country[i], "  " );
                char country[LENGTH_COUNTRY];
                strcpy(country,"DEFAULT COUNTRY"); // Used when player has a invalid country code
                int j;
                for(j=0;j<NUM_COUNTRIES;j++)
                {
                        if(strcmp(country_abbreviations[i],country_code[j])==0)
                                strcpy(country,country_name[j]);
                }
                strcat(name_and_country[i],country);
                printf("%s\n",name_and_country[i]);
        }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM