50026. A Better Word Count

難度:3/5

Syntax記一下,還有數字數的方法也能記一下,其他應該就沒什麼大礙。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

int count_line(char* file_name){
FILE *f = fopen(file_name, "r");
int cnt = 0;

char buf[2000];
while(fgets(buf, 2000, f)){
cnt++;
}
return cnt;
}

int count_word(char* file_name){
FILE *f = fopen(file_name, "r");
int cnt = 0;

char buf[2000];
while(fgets(buf, 2000, f)){
int temp_cnt = 0;

int n = strlen(buf);
for(int i = 0; i < n; i++){
if(isalpha(buf[i])){
while(isalpha(buf[i])){
i++;
}
cnt++;
}
}
}

return cnt;
}

int count_size(char* file_name){
FILE *f = fopen(file_name, "r");
fseek(f, 0, SEEK_END);
int res = ftell(f);
fclose(f);
return res;
}

int main(){
char file_name[1024];
scanf("%s", file_name);

printf("%d %d %d\n", count_line(file_name), count_word(file_name), count_size(file_name));
}


50026. A Better Word Count
https://aaronlin1229.github.io/judgegirl_50026/
Author
Akizumi
Posted on
July 17, 2023
Licensed under