gpt4 book ai didi

c - 如何在 C 编程中将两个字符串数组放在一起

转载 作者:行者123 更新时间:2023-11-30 15:47:34 24 4
gpt4 key购买 nike

我有这个项目。我有包含分数、比赛 1、比赛 2 的数组,还有球员的姓名及其国家/地区缩写。然后我就有了这些缩写和国家的长名称。

我可以将玩家姓名和国家/地区缩写放在一起,也可以将国家/地区缩写和国家/地区长名称放在一起。

但我不知道如何将国家/地区长名称和球员姓名放在一个数组中。

因此,我创建了数组 (char name_and_country[PLAYERS][LENGTH_NAME]),我希望此数组中包含名称和国家/地区;所以如果我打印出来说:

char name_and_country[PLAYERS][LENGTH_NAME]={“大卫·贝克汉姆英格兰”、“韦恩·鲁尼英格兰”......等等。

有人可以帮我吗?预先感谢您!

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

关于c - 如何在 C 编程中将两个字符串数组放在一起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17311858/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com