我需要用C编写一个程序,它接受命令行输入,将其放入一个字符串数组,并以相反的方式打印该数组 #包括 #包括string.h int concat(char str[],char*lngstring[],int x); void printreverse(字符lngstring[],int x); #定义最大字符数100 int main(int argc,char*argv[]){ longstring[MAX_CHARS]={'\0'}; concat(*argv[],*longstring[],argc); printf(“%s”,长串); printreverse(*longstring[],argc); } int concat(char*str[],char*lngstring[],int x){ 整数计数=0; int count2=0; 虽然(count
好的,我给你点实际尝试的分数,不仅仅是要求我们为你写这篇文章,但我必须说你做得太多了。根据你的描述,你需要的代码可以简单得多。不过,我不想为你做这件事,所以我给你一些建议:我需要用C编写一个程序,它接受命令行输入,将其放入一个字符串数组,并以相反的方式打印该数组 #包括 #包括string.h int concat(char str[],char*lngstring[],int x); void printreverse(字符lngstring[],int x); #定义最大字符数100 int main(int argc,char*argv[]){ longstring[MAX_CHARS]={'\0'}; concat(*argv[],*longstring[],argc); printf(“%s”,长串); printreverse(*longstring[],argc); } int concat(char*str[],char*lngstring[],int x){ 整数计数=0; int count2=0; 虽然(count,c,arrays,string,command-line-arguments,C,Arrays,String,Command Line Arguments,好的,我给你点实际尝试的分数,不仅仅是要求我们为你写这篇文章,但我必须说你做得太多了。根据你的描述,你需要的代码可以简单得多。不过,我不想为你做这件事,所以我给你一些建议: arg已经存储在一个字符串数组中,因此您不必创建单独的字符串数组来存储它们。只需使用argv即可 您的循环逻辑太复杂,无法简单地在数组中循环。当您只需在数组中循环一次时,将for循环嵌套在while循环中是完全不必要的。向前遍历:for(int i=0;i=0;i--) 缩进代码。将问题分解为子问题(您已经识别了它们-获取
- arg已经存储在一个字符串数组中,因此您不必创建单独的字符串数组来存储它们。只需使用argv即可
- 您的循环逻辑太复杂,无法简单地在数组中循环。当您只需在数组中循环一次时,将for循环嵌套在while循环中是完全不必要的。向前遍历:
。向后遍历:for(int i=0;i
for(int i=len-1;i>=0;i--)
string.h
具有strcat
,因此您不需要编写它。如果您在web上搜索,这个常见的家庭作业问题的点击率会很高。这样,我就不需要将数组复制到新数组中了?我需要使用一个有趣的指针修改数组对吗?还要感谢您迄今为止的帮助,特别是对循环的建议。不需要修改数组。您只需向后遍历它,并在运行时打印其中的每个值。
#include <stdio.h>
#include string.h
int concat(char str[], char *lngstring[], int x);
void printreverse(char lngstring[], int x);
#define MAX_CHARS 100
int main( int argc, char *argv[] ) {
longstring[MAX_CHARS] = {'\0'};
concat(*argv[], *longstring[],argc);
printf("%s", longstring);
printreverse(*longstring[],argc);
}
int concat(char *str[], char *lngstring[], int x){
int count = 0;
int count2 = 0;
while(count <= (2 * x){
lngstring[count] = str[count2];
count += 1;
lngstring[count] = " ";
count += 1;
count2 += 1;
}
return 0;
}
void printreverse(char *lngstring[], int x){
char *cas[] = lngstring;
int count = 0;
int count2 = x;
char temp[];
char temp2[];
while(count <= x){
for(i = 0; i < x; i++){
char temp[] = cas[count];
char temp2[] = cas[count2];
cas[count2] = temp;
cas[count] = temp2;
count++;
count2--;
}
//reverse the order of strings in array
count = 0;
while(count <= x){
int bs = strlen(cas[count]);
int zx = bs;
char temp[] = cas[count]
for(i = 0; i <= bs; i++){
char temps = temp[i]
char temps2 = temp[zx]
temp[i] = temps2;
temp[zx] = temps;
zx--;
}
//reverse each individual string
cas[count] = temp;
}
printf("%s",cas)
}