2017-10-12 32 views
-2
#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) 
} 

此程序阵列/打印需要采取命令行参数,它们连接成一个字符串数组,打印该数组的每个元素,并反向打印该数组的每个元素。我真的不知道我在做什么,这是我最好的尝试。我很难掌握指针以及何时以及如何使用它们。任何帮助将不胜感激。我需要用C来写一个程序,它命令行输入,将其放入一个字符串数组,并打印在反向

+2

缩进代码。将问题分解为子问题(您已经确定了它们 - 接受参数,连接,反转,打印)。并逐一解决。 –

+1

参数已经在字符串数组中。 –

+0

请注意'string.h'有'strcat',所以你不需要编写它。 –

回答

1

好的,我给你点实际尝试,而不是要求我们为你写这个,但我必须说你做得太多。根据您的描述,您需要的代码可以简单得多。我不想为你做,虽然如此,我给你一些指点:

  • 的ARG游戏已经存储在一个字符串数组,所以你不必做一个单独的数组的字符串来存储它们。只需使用argv。

  • 您的循环逻辑过于复杂,仅仅是循环遍历数组。当你需要做的只是循环一个数组时,在while循环中嵌套一个for循环是不必要的。要通过它前进:for(int i = 0; i < len; i++)。向后翻:for(int i=len-1; i >=0; i--)

+0

所以我不需要将数组复制到一个新的数组?我需要使用函数中的指针修改数组吗?另外感谢您的帮助,尤其是对于循环的建议。 –

+0

没有必要修改数组。您可以向后遍历它,并随时打印其中的每个值。 –

相关问题