-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest2.c
199 lines (177 loc) · 3.78 KB
/
test2.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*********************************************************************************
* Copyright: (C) 2017 tangyanjun<[email protected]>
* All rights reserved.
*
* Filename: myfun.c
* Description: This file
*
* Version: 1.0.0(07/27/2017)
* Author: tangyanjun <[email protected]>
* ChangeLog: 1, Release initial version on "07/27/2017 03:46:24 PM"
*
********************************************************************************/
#include <stdio.h>
#include <string.h>
/* realize of strlen*/
void mystrlen(char data[])
{
int i = 0;
int len = 0;
for(i = 0; data[i] != '\0'; i++)
{
len++;
}
printf("len = %d\n", len);
}
/* realize of strcpy and strncpy */
void mystrcpy(char data[], char data1[])
{
int i = 0;
for(i = 0; data[i] != '\0'; ++i)
{
data1[i] = data[i];
}
data1[i] = '\0';
printf("%s\n", data1);
}
void mystrncpy(char data[], char data1[], int n) //复制前n个字符
{
int i = 0;
for(i = 0; i < n && data[i] != '\0'; ++i)
{
data1[i] = data[i];
}
data1[i] = '\0';
printf("%s\n", data1);
}
/* realize of strcat and strncat */
void mystrcat(char data[], char data1[])
{
int len = strlen(data);
int len1 = strlen(data1);
int i;
for(i = 0; data1[i] != '\0'; ++i)
{
data[len + i] = data1[i];
}
printf("%s\n", data);
}
void mystrncat(char data[], char data1[], int n) //追加n个字符
{
int len = strlen(data);
int len1 = strlen(data1);
int i;
for(i = 0; i < n && data1[i] != '\0'; ++i)
{
data[len + i] = data1[i];
}
printf("%s\n", data);
}
int mystrcmp(char data[], char data1[])
{
int i;
for(i = 0; ; i++)
{
if(data[i] < data1[i])
{
return -1;
}
else if(data[i] > data1[i])
{
return 1;
}
else if(data[i] == '\0')
{
return 0;
}
}
}
int mystrncmp(char data[], char data1[], int n)
{
int i;
for(i = 0; i < n; i++)
{
if(data[i] < data1[i])
{
return -1;
}
else if(data[i] > data1[i])
{
return 1;
}
else if(data[i] == data1[i])
{
return 0;
}
}
}
void mystrstr(char data[], char data1[])
{
char* index = NULL;
index = data;
char* str1 = NULL;
char* str2 = NULL;
if(*data1 == '\0')
{
printf("The data1 is NULL!\n");
}
while(*index != '\0')
{
str1 = index;
str2 = data1;
while(*str1 != '\0' && *str2 != '\0' && (*str1 - *str2) == 0)
{
str1++;
str2++;
}
if(*str2 == '\0')
{
printf("%s\n", index);
}
index++;
}
}
int main(int argc, char argv)
{
char data[100] = "Hellosssssssssssss";
char data1[100] = "llosssss";
mystrlen(data);
// mystrcpy(data, data1);
// mystrncpy(data, data1, 2);
// mystrcat(data, data1);
// mystrncat(data, data1, 2);
mystrstr(data, data1);
/*
int res = mystrcmp(data, data1);
switch(res)
{
case 1:
printf("data > data1\n");
break;
case -1:
printf("data < data1\n");
break;
case 0:
printf("data == data1\n");
break;
default:
break;
}
int res1 = mystrncmp(data, data1, 1);
switch(res1)
{
case 1:
printf("data > data1\n");
break;
case -1:
printf("data < data1\n");
break;
case 0:
printf("data == data1\n");
break;
default:
break;
}
*/
return 0;
}