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

This program needs to take command line arguments, concatenate them into a string array, print every element of that array, and print every element of that array in reverse. I really have no idea what I'm doing and this is my best attempt at it. I'm having a hard time grasping pointers and when and how to use them. Any help would be greatly appreciated.

Christian Gibbons
  • 4,272
  • 1
  • 16
  • 29
T.Peck
  • 1
  • 1

1 Answers1

1

Okay, I give you points for actually trying and not just asking us to write this for you, but I must say you're doing too much. The code you need, based on your description, can be much simpler. I don't want to do it for you, though, so I'll give you some pointers:

  • The args are already stored in a string array, so you don't have to make a separate array of strings to store them. Just use argv.

  • Your looping logic is way too convoluted for simply looping through an array. Nesting a for-loop inside a while-loop when all you need to do is loop through an array one time is quite unnecessary. To go through it forward: for(int i = 0; i < len; i++). To go through it backwards: for(int i=len-1; i >=0; i--)

Christian Gibbons
  • 4,272
  • 1
  • 16
  • 29
  • So I dont need to copy the array into a new array? I would need to modify the array using a pointer in a function right?Also Thank you for the help so far, especially the suggestions with the loops. – T.Peck Oct 12 '17 at 18:11
  • There is no need to modify the array. You can just traverse it backwards and print each value from it as you go. – Christian Gibbons Oct 12 '17 at 18:14