-
Notifications
You must be signed in to change notification settings - Fork 0
/
10_2_1.c
99 lines (85 loc) · 1.87 KB
/
10_2_1.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
89
90
91
92
93
94
95
96
97
98
99
/*
单字符输入输出
putchar
int putchar(int c);
向标准输出写一个字符
返回写了几个字符,EOF(-1)表示失败 end of fail
getchar
int getchar(void);
从标准输入读入一个字符
返回类型时int为了返回EOF(-1)
win -> ctrl+z
unix -> ctrl+d
键盘-shell-程序
行编辑
字符串函数STRLEN
string.h
strlen strcmp strcpy
strcat strchr strstr
strlen 返回s的字符串长度,不包括结尾的0
size_t strlen(const char *s);
strcmp compare 比较两个字符串
int strcmp(const char *s1, const char *s2)
0: s1 = s2
1: s1>s2
-1: s1<s2
s1 == s2比较的是地址
每一个s1[] - s2[]
*/
#include <stdio.h>
size_t mylen (const char* s)//strlen
{
int cnt = 0;
while (s[cnt] != '\0')
{
cnt++;
}
}
int mycmp(const char* s1, const char* s2)
{
//常见两种方法
//当作数组,用一个整数当下标
//int idx = 0;
// while ( 1 )
// {
// if ( s1[idx] == s2[idx] )
// {
// break;
// }
// else if ( s1[idx] == '\0')
// {
// break;
// }
// idx++;
// }
// return s1[idx] - s2[idx];
// while ( s1[idx] == s2[idx] && s1[idx] != '\0')
// {
// // if ( s1[idx] == s2[idx] )
// // {
// // break;
// // }
// // else if ( s1[idx] == '\0')
// // {
// // break;
// // }
// idx++;
// }
//直接用指针
while ( *s1 == *s2 && *s1 != '\0')
{
s1++;
s2++;//两种编译器代码可能差不多
}
//return s1[idx] - s2[idx];
return *s1 - *s2;
}
int main (int argc, char const *argv[])
{
int ch;
while ( (ch = getchar()) != EOF )
{
putchar(ch);
}
return 0;
}