tweak: use else if instead of wrapped else, if

+formatting
This commit is contained in:
NunoSempere 2023-09-15 12:19:40 +03:00
parent 926eab1a9b
commit c3ac29d887
4 changed files with 15 additions and 17 deletions

View File

@ -6,7 +6,7 @@ int chc(FILE* fp)
register int c; register int c;
int num_chars = 0; int num_chars = 0;
while ((c = getc(fp)) != EOF) { while ((c = getc(fp)) != EOF) {
num_chars ++; num_chars++;
} }
printf("%i\n", num_chars); printf("%i\n", num_chars);
return 0; return 0;

View File

@ -6,8 +6,8 @@ int lc(FILE* fp)
int num_lines = 0; int num_lines = 0;
register int c; register int c;
while ((c = getc(fp)) != EOF) { while ((c = getc(fp)) != EOF) {
if (c == '\n' ) { if (c == '\n') {
num_lines ++; num_lines++;
} }
} }
// num_lines += (c != '\n'); // < judgment call, adds a line if the last line isn't followed by a newline. // num_lines += (c != '\n'); // < judgment call, adds a line if the last line isn't followed by a newline.

BIN
src/wc

Binary file not shown.

View File

@ -8,14 +8,12 @@ int wc(FILE* fp)
while ((c = getc(fp)) != EOF) { while ((c = getc(fp)) != EOF) {
if (c != ' ' && c != '\n' && c != '\t') { if (c != ' ' && c != '\n' && c != '\t') {
word = 1; word = 1;
} else { } else if (word) {
if (word) {
num_words++; num_words++;
word = 0; word = 0;
} }
} }
} num_words += word;
num_words+=word;
printf("%i\n", num_words); printf("%i\n", num_words);
return 0; return 0;
} }
@ -32,7 +30,7 @@ int main(int argc, char** argv)
} }
int wc_status = wc(fp); int wc_status = wc(fp);
int fclose_status = fclose(fp); int fclose_status = fclose(fp);
return (wc_status == 0) && (fclose_status ==0); return (wc_status == 0) && (fclose_status == 0);
// can't do return wc_status == 0 && fclose_status == 0; // can't do return wc_status == 0 && fclose_status == 0;
// because then if wc returns with -1, fclose is not called. // because then if wc returns with -1, fclose is not called.
} else { } else {