-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfind_string.c
92 lines (75 loc) · 1.59 KB
/
find_string.c
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define MAXLINE 1000
int get_line(char line[], int maxline);
int str_index(char source[], char searchfor[]);
char pattern[] = "ould";
int my_atoi(char s[]);
int my_atoi2(char s[]);
int my_lower(int c);
double my_atof(char s[]);
int main()
{
char line[MAXLINE];
int found = 0;
while (get_line(line, MAXLINE) > 0)
if (str_index(line, pattern) >= 0) {
printf("%s", line);
found++;
}
return found;
}
/*函数实体中的变量名称可以和前面declared的不同*/
int get_line(char s[], int lim)
{
int c, i;
for (i = 0; i < lim-1 && (c = getchar()) != EOF && c != '\n'; ++i)
s[i] = c;
printf("the 'i' after the for loop is %d\n", i);
if (c == '\n') {
s[i] = c;
++i;
}
s[i] = '\0'; // 字符串的结尾必须是\0, 没有实际意义 但是占用内存空间
return i;
}
/*这里function的argument名称还是和declarition时不同,不影响*/
int str_index(char s[], char t[])
{
int i, j, k;
for (i = 0; s != '\0'; i++) {
for (j = i, k = 0; t[k] != '\0' && s[j] == t[k]; j++, k++)
;
}
}
int my_atoi(char s[])
{
int i, n;
n = 0;
for (i = 0; s[i] >= '0' && s[i] <= '9'; ++i)
n = 10 * n + (s[i] - '0');
return n;
}
int my_atoi2(cahr s[])
{
/* C不允许在一个函数之内define另一个函数
但是可以declare另一个函数*/
double atof(char s[]);
return (int) ator(s);
}
int my_lower(int c)
{
if (c >= 'A' && c <= 'Z')
return c - 'A' + 'a';
else
return c;
}
double my_atof(char s[])
{
double val, power;
int i, sign;
for (i = 0; isspace(s[i]); i++)
;
sign = (s[i] == '-') ? -1 : 1;
}