Home > OS >  Reverse items in an array of strings in C
Reverse items in an array of strings in C

Time:07-09

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, "           
  • Related