[ create a new paste ] login | about

Link: http://codepad.org/7Dng7MtM    [ raw code | output | fork ]

C, pasted on Jun 20:
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
int STRLEN(char *s)
{
	int count = 0;
	while (s[count] != '\0')
	{
		count++;
	}
	return count;
}

void STRCPY(char *s1, char *s2)
{
	int length = STRLEN(s2);
	for (int i = 0; i < length; i++)
	{
		s1[i] = s2[i];
	}
	s1[length] = '\0';
}

void STRLWR(char *s)
{
	int length = STRLEN(s);
	for (int i = 0; i < length; i++)
	{
		if (s[i] >= 'A' && s[i] <= 'Z')
		{
			s[i] += 32;
		}
	}
}
void STRUPR(char *s)
{
	int length = STRLEN(s);
	for (int i = 0; i < length; i++)
	{
		if (s[i] >= 'a' && s[i] <= 'z')
		{
			s[i] -= 32;
		}
	}
}

void HoanVi(char &a, char &b)
{
	char Temp = a;
	a = b;
	b = Temp;
}

void STRREV(char *s)
{
	int length = STRLEN(s);
	for (int i = 0; i < length / 2; i++)
	{
		HoanVi(s[i], s[length - 1 - i]);
	}
}

int STRCMP(char *s1, char *s2)
{
	int length1 = STRLEN(s1);
	int length2 = STRLEN(s2);

	int LengthMin = length1 < length2 ? length1 : length2;

	for (int i = 0; i < LengthMin; i++)
	{
		if(s1[i] < s2[i])
		{
			return -1;
		}
		else if(s1[i] > s2[i])
		{
			return 1;
		}
	}
	if (length1 > length2)
	{
		return 1;
	}
	else if (length1 < length2)
	{
		return -1;
	}
	return 0;
}

int STRICMP(char *s1, char *s2)
{
	/*STRLWR(s1);
	STRLWR(s2);
	return STRCMP(s1, s2);*/

	int length1 = STRLEN(s1);
	int length2 = STRLEN(s2);

	int LengthMin = length1 < length2 ? length1 : length2;

	for (int i = 0; i < LengthMin; i++)
	{
		char temp1 = s1[i];
		char temp2 = s2[i];

		// Quy hết tất cả về ký tự thường
		if(temp1 >= 'A' && temp1 <= 'Z')
		{
			temp1 += 32;
		}
		if(temp2 >= 'A' && temp2 <= 'Z')
		{
			temp2 += 32;
		}

		if( temp1 < temp2)
		{
			return -1;
		}
		else if(temp1 > temp2)
		{
			return 1;
		}
	}
	if (length1 > length2)
	{
		return 1;
	}
	else if (length1 < length2)
	{
		return -1;
	}
	return 0;
}

void STRCAT(char *s1, char *s2)
{
	int length1 = STRLEN(s1);
	int size1 = sizeof(s1);
	int idx = 0;

	int length2 = STRLEN(s2);
	for (int i = 0; i < length2; i++)
	{
		s1[length1++] = s2[i];
		if (length1 == size1 - 1)
		{
			break;
		}
	}
}

int STRSTR(char *s1, char *s2)
{
	int length1 = STRLEN(s1);
	int length2 = STRLEN(s2);
	int Start;
	bool Check;

	for (int i = 0; i < length1; i++)
	{
		if (s1[i] == s2[0])
		{
			Start = i;
			Check = true;
			int Temp = Start;
			for (int j = 1; j < length2; j++)
			{
				if (s1[++Temp] != s2[j])
				{
					Check = false;
					break;
				}
			}
			if(Check == true)
			{
				return Start;
			}
		}
	}
	return -1;
}

/*
strdup(s): Sao chép chuỗi s bỏ qua vùng
nhớ mới có độ lớn vừa đủ chứa

độ lớn sao chép: length + 1
*/

char * STRDUP(char *s)
{
	int length = STRLEN(s);
	char *p = (char *)malloc((length + 1) * sizeof(char));

	for (int i = 0; i < length; i++)
	{
		p[i] = s[i];
	}
	p[length] = '\0';
	return p;
}
/*
"1234"
Lần lượt lấy từng ký tự ra rồi đổi sang số
tương ứng

ta thấy kết quã mong muốn là 1234
1234 = 1 * 10^3 + 2 * 10^2 + 3 * 10^1 + 4 * 10^0;
*/

int ATOI(char *s)
{
	int tong = 0;

	int length = STRLEN(s);
	int Temp = length;
	// Xác định temp để biết phạm vi mình sẽ duyệt

	for (int i = 0; i < Temp; i++)
	{
		if (s[i] < '0' || s[i] > '9') // Không phãi là số
		{
			Temp = i;
			break;
		}
	}
	int Temp1 = Temp;

	for (int i = 0; i < Temp; i++)
	{
		tong += (s[i] - 48) * pow(10.0, --Temp1);  // đổi ký tự sang số rồi nhân cho 10 mũ ...
	}
	return tong;
}

double ATOF(char *s)
{
	char phannguyen[30];
	int idx = 0;
	char phanle[30];
	int length = STRLEN(s);
	bool Check = true;

	for (int i = 0; i < length; i++)
	{
		if (Check == true)
		{
			phannguyen[idx++] = s[i];
		}
		else
		{
			phanle[idx++] = s[i];
		}
		if (s[i] == '.')
		{
			Check = false;
			idx = 0;
		}
	}
	return ATOI(phannguyen) + ATOI(phanle) / pow(10.0, idx);
}

void ITOA(int value, char *ketqua, int coso)
{
	int idx = 0;
	while(value != 0)
	{
		int so = value % coso;
		if (so < 9)
		{
			ketqua[idx++] = so + 48;
		}
		else if(so == 10)
		{
			ketqua[idx++] = 'A';
		}
		else if(so == 11)
		{
			ketqua[idx++] = 'B';
		}
		else if(so == 12)
		{
			ketqua[idx++] = 'C';
		}
		else if(so == 13)
		{
			ketqua[idx++] = 'D';
		}
		else if(so == 14)
		{
			ketqua[idx++] = 'E';
		}
		else if(so == 15)
		{
			ketqua[idx++] = 'F';
		}
		value /= coso;
	}
	ketqua[idx] = '\0';
	STRREV(ketqua);
}
// SUBSTR(s, x, y) // Cắt chuỗi con trong đoạn [x, y] của chuỗi s
char *SUBSTR(char *s, int x, int y)
{
	char ketqua[100];
	int length = STRLEN(s);
	int idx = 0;
	for (int i = x; i <= y; i++)
	{
		ketqua[idx++] = s[i];
	}
	ketqua[idx] = '\0';
	return ketqua;
}

int TimViTriDauTien(char *s, char *find)
{
	int length_s = STRLEN(s);
	int length_find = STRLEN(find);
	int Start;
	bool Check;

	for (int i = 0; i < length_s; i++)
	{
		if (s[i] == find[0])
		{
			Start = i;
			Check = true;
			int Temp = Start;
			for (int j = 1; j < length_find; i++)
			{
				if (find[j] != s[++Temp])
				{
					Check = false;
					break;
				}
			}
			if (Check == true)
			{
				return Start;
			}
		}
	}
	return -1;
}
int main()
{
	char s[] = "Vuong Tri Tai";
	int length = STRLEN(s);
	printf("\nlength = %d",length);


	char s1[30];
	STRCPY(s1,"Tai dep trai");
	printf("\n %s",s1);

	STRLWR(s);
	printf("\nChu thuong: %s", s);

	STRUPR(s);
	printf("\nChu hoa: %s", s);

	STRREV(s);
	printf("\nChuoi dao nguoc: %s", s);


	char s3[] = "abcd";
	char s4[] = "Abed";

	int kq = STRCMP(s3,s4);
	printf("\nKq = %d",kq);

	int kq2 = STRICMP(s3,s4);
	printf("\nKq2 = %d",kq2);

	char s5[20] = "abc";
	char s6[7] = "defghi";

	STRCAT(s5,s6);
	printf("\ns5 = %s",s5);

	char s7[] = "Nguyen Viet Nam Son";
	char s8[] = "Nam";

	int ViTri = STRSTR(s7,s8);
	printf("\nVi tri = %d",ViTri);

	char s9[] = "Son xau trai";
	char *p  = STRDUP(s9);
	printf("\np = %s",p);

	free(p);


	char s10[] = "1275";

	int x = ATOI(s10);
	printf("\nx = %d", x);

	char s11[] = "1275.54654";
	double y = ATOF(s11);
	printf("\ny = %lf", y);

	int value = 250;
	char ketqua[100];
	int coso = 16;

	ITOA(value,ketqua,coso);
	printf("\nket qua = %s", ketqua);

	char s12[] = "Nguyen Viet Nam Son";
	char sub[100];
	strcpy(sub,SUBSTR(s12,7,10));

	printf("\nsub = %s", sub);

	char s13[] = "Nguyen Viet Nam Son";
	int ViTriDauTien = TimViTriDauTien(s13, "Viet");
	printf("\nVitri = %d", ViTriDauTien);

	getch();
	return 0;
}


Output:
Line 17: error: conio.h: No such file or directory
In function 'STRCPY':
Line 19: error: 'for' loop initial declaration used outside C99 mode
In function 'STRLWR':
Line 29: error: 'for' loop initial declaration used outside C99 mode
In function 'STRUPR':
Line 40: error: 'for' loop initial declaration used outside C99 mode
t.c: At top level:
Line 49: error: expected ';', ',' or ')' before '&' token
In function 'STRREV':
Line 59: error: 'for' loop initial declaration used outside C99 mode
In function 'STRCMP':
Line 72: error: 'for' loop initial declaration used outside C99 mode
In function 'STRICMP':
Line 105: error: 'for' loop initial declaration used outside C99 mode
In function 'STRCAT':
Line 147: error: 'for' loop initial declaration used outside C99 mode
In function 'STRSTR':
Line 162: error: 'bool' undeclared (first use in this function)
Line 162: error: (Each undeclared identifier is reported only once
Line 162: error: for each function it appears in.)
Line 162: error: expected ';' before 'Check'
Line 164: error: 'for' loop initial declaration used outside C99 mode
Line 169: error: 'Check' undeclared (first use in this function)
Line 169: error: 'true' undeclared (first use in this function)
Line 171: error: 'for' loop initial declaration used outside C99 mode
Line 175: error: 'false' undeclared (first use in this function)
In function 'STRDUP':
Line 200: error: 'for' loop initial declaration used outside C99 mode
In function 'ATOI':
Line 224: error: 'for' loop initial declaration used outside C99 mode
Line 234: error: redefinition of 'i'
Line 224: error: previous definition of 'i' was here
Line 234: error: 'for' loop initial declaration used outside C99 mode
In function 'ATOF':
Line 247: error: 'bool' undeclared (first use in this function)
Line 247: error: expected ';' before 'Check'
Line 249: error: 'for' loop initial declaration used outside C99 mode
Line 251: error: 'Check' undeclared (first use in this function)
Line 251: error: 'true' undeclared (first use in this function)
Line 261: error: 'false' undeclared (first use in this function)
In function 'SUBSTR':
Line 313: error: 'for' loop initial declaration used outside C99 mode
Line 318: warning: function returns address of local variable
In function 'TimViTriDauTien':
Line 326: error: 'bool' undeclared (first use in this function)
Line 326: error: expected ';' before 'Check'
Line 328: error: 'for' loop initial declaration used outside C99 mode
Line 333: error: 'Check' undeclared (first use in this function)
Line 333: error: 'true' undeclared (first use in this function)
Line 335: error: 'for' loop initial declaration used outside C99 mode
Line 339: error: 'false' undeclared (first use in this function)


Create a new paste based on this one


Comments: