Home > Enterprise >  Append '0' into array of string until it reaches a certain amount
Append '0' into array of string until it reaches a certain amount

Time:07-13

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 put on a int variable, the numbers from the second line onwards are on an array of strings. Then created another string with its content reversed.Ps: second number is the size of the strings in the array.
My problem is that, if the first number is higher than the number of strings in the array, it's suppose to fill the array with a number of strings of 0's(of the size of the second number), until the number of strings on the array is the same of the first number. If the first number is equal or lower than the number of strings in the array, do not insert anything else, and just leave the string as it is.
I'll put an exemple bellow.

Input

7 8
10101011
10000011
11111111
11111111

Output

List of Numbers on Array:
10101011
10000011
11111111
11111111

List of Numbers Reversed on Array:
11111111
11111111
10000011
10101011

List of Numbers Reversed on Array after Inserting the rest:
11111111
11111111
10000011
10101011
00000000
00000000
00000000
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