This repository was archived by the owner on Feb 18, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.c
More file actions
72 lines (60 loc) · 1.45 KB
/
solution.c
File metadata and controls
72 lines (60 loc) · 1.45 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/* 单词统计 for problem 246 on XDOJ by LyCecilion - Pure C version */
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum
{
MAX_LINE = 102, /* 100 chars + '\n' + '\0' */
MAX_WORD = 10, /* 8 chars + '\n' + '\0' */
};
static void chomp(char *s)
{
size_t n = strlen(s);
if (n && s[n - 1] == '\n')
s[n - 1] = '\0';
}
static int ci_word_eq(const char *s, size_t len, const char *w)
{
for (size_t i = 0; i < len; i++)
{
if (w[i] == '\0')
return 0; // length not match, w is shorter
if (tolower((unsigned char)s[i]) != tolower((unsigned char)w[i]))
return 0;
}
return w[len] == '\0'; // length not match, s is shorter
}
static int count(const char *line, const char *word)
{
int cnt = 0;
const char *p = line;
while (*p)
{
while (*p && isspace((unsigned char)*p))
p++;
if (!p)
break;
const char *start = p;
while (*p && !isspace((unsigned char)*p))
p++;
size_t len = (size_t)(p - start);
if(ci_word_eq(start, len, word))
cnt++;
}
return cnt;
}
int main(void)
{
char line[MAX_LINE];
char word[MAX_WORD];
if (!fgets(line, sizeof(line), stdin))
return 0;
if (!fgets(word, sizeof(word), stdin))
return 0;
chomp(line);
chomp(word);
int c = count(line, word);
printf("%s %d\n", word, c);
return 0;
}