1

I need help on my code. I have done a code (below) to read the numbers from a .txt file. The first two numbers are to be put on a int variable, the numbers from the second line onwards are to be put on an array of strings. But now, i want to reverse the array of strings and put in a new array of strings. I've been having a problem trying to solve this but i'm not being able to.

Ex. File:

 3 8
 10000001
 11101001
 00100101
 11000010

i need: listOfNumbersReversed = {"11000010","00100101","11101001","10000001"}

Code:

int main() {
FILE *file = fopen("file.txt", "r");
if (file == NULL) {
    fprintf(stderr, "Cannot open file.txt: %s\n", strerror(errno));
    return 1;
}
// read the 2 numbers
int primNum, secNum;
if (fscanf(file, "%d %d\n", &primNum, &secNum) != 2) {
    fprintf(stderr, "invalid file format\n");
    fclose(file);
    return 1;
}
// count the number of items that can be read
char line[100];
int counter;
for (counter = 0; fscanf(file, "%99s", line) == 1; counter++)
     continue;

printf("Total number of items: %d\n", counter);

// Rewind and re-read the contents into the array
rewind(file);
char listOfNumbers[counter][100];
int i;
if (fscanf(file, "%d %d\n", &primNum, &secNum) != 2) {
    fprintf(stderr, "cannot reread the numbers\n");
    fclose(file);
    return 1;
}
for (i = 0; i < counter; i++) {
    if (fscanf(file, "%99s", listOfNumbers[i]) != 1) {
        // Cannot read all the numbers file changed ?
        printf("could only read %d numbers out of %d\n", i, counter);
        counter = i;
        break;
    }
}

// Testing Results
printf("1st Number: %d\n", primNum);
printf("2nd Number: %d\n\n", secNum);
printf("List of Numbers on Array:\n");
for (i = 0; i < counter; i++) {
    printf("%s\n", listOfNumbers[i]);
}
fclose(file);

//Reversing the array of strings
char listOfNumbersReversed[counter][secNum];
for(i = counter - 1; i>=0; i--){
    int j = 0;
    memcpy(&listOfNumbersReversed[j], &listOfNumbers[i], secNum);
    j++;
}

//Testing Results
printf("\n\nList of Numbers Reversed on Array:\n");
for (i = 0; i < counter; i++) {
    printf("%s\n", listOfNumbersReversed[i]);
}

return 0;
}

Ps: the secNum variable is the size of the itens in the array

1
  • 1
    1. In order to reverse an array of strings you need to loop till the middle of it, and swap oposing elements. 2. j++ has no effect in your reversing loop because it is set to 0 in the beginning of each iteration and you don't use it after the increment. Commented Jul 9, 2022 at 3:10

1 Answer 1

3

You are really close but here are a few things.

  1. move the j variable outside the loop so it isn't reset to zero every time through the loop.
  2. add space in the array to store the '\0' string termination.
  3. add 1 to the number of characters to copy
    char listOfNumbersReversed[counter][secNum+1];
    int j = 0;
    for (i = counter - 1; i >= 0; i--)
    {
      memcpy (&listOfNumbersReversed[j][0], &listOfNumbers[i][0], secNum+1);
      j++;
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.