Name: Anonymous 2020-12-24 3:41
The following code is supposed to take a .txt file like the one below:
https://pastebin.com/HfjkVZpy
It will then go by each line and see if each password is valid by the amount of times the letter appears. For example, "1-9 a: aaaer" is valid, but "1-2 q: aqeeqgqf" is not".
#include <stdio.h>
#include <string.h>
int main( int argc, char *argv[])
{
FILE *file;
int i = 0;
int file_line_count;
int valid_passwords = 0;
file = fopen(argv[1], "r");
typedef struct node {
char ranges[4];
char filter_letter[3];
char password[51];
} Password_Verification;
if (file) {
for (char c = getc(file); c != EOF; c = getc(file)) {
if (c == '\n') file_line_count = file_line_count + 1;
}
Password_Verification Pswds[file_line_count + 1];
while (3 == fscanf(file, " %4c %3c %51c", &Pswds[i].ranges,
&Pswds[i].filter_letter,
&Pswds[i].password))
{
int k = 0;
for (int j = 0; j < strlen(Pswds[i].password); j++) {
if (Pswds[i].password[j] == Pswds[i].filter_letter[0]) {
k = k + 1;
if ( (k >= (int)(Pswds[i].ranges[0] - 48)) && (k <= (int)(Pswds[i].ranges[2] - 48)) ) {
valid_passwords = valid_passwords + 1;
}
}
continue;
}
}
printf("%d\n", valid_passwords);
return 0;
}
}
But for some reason, when I run this code with the input above, the valid_passwords factor never increments. Why is this the case?
https://pastebin.com/HfjkVZpy
It will then go by each line and see if each password is valid by the amount of times the letter appears. For example, "1-9 a: aaaer" is valid, but "1-2 q: aqeeqgqf" is not".
#include <stdio.h>
#include <string.h>
int main( int argc, char *argv[])
{
FILE *file;
int i = 0;
int file_line_count;
int valid_passwords = 0;
file = fopen(argv[1], "r");
typedef struct node {
char ranges[4];
char filter_letter[3];
char password[51];
} Password_Verification;
if (file) {
for (char c = getc(file); c != EOF; c = getc(file)) {
if (c == '\n') file_line_count = file_line_count + 1;
}
Password_Verification Pswds[file_line_count + 1];
while (3 == fscanf(file, " %4c %3c %51c", &Pswds[i].ranges,
&Pswds[i].filter_letter,
&Pswds[i].password))
{
int k = 0;
for (int j = 0; j < strlen(Pswds[i].password); j++) {
if (Pswds[i].password[j] == Pswds[i].filter_letter[0]) {
k = k + 1;
if ( (k >= (int)(Pswds[i].ranges[0] - 48)) && (k <= (int)(Pswds[i].ranges[2] - 48)) ) {
valid_passwords = valid_passwords + 1;
}
}
continue;
}
}
printf("%d\n", valid_passwords);
return 0;
}
}
But for some reason, when I run this code with the input above, the valid_passwords factor never increments. Why is this the case?