/
Lab08_MccaddenD.c
70 lines (50 loc) · 1.11 KB
/
Lab08_MccaddenD.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
#include <stdio.h>
int *arr_first_max(const int *a, size_t n);
int *arr_last_max(const int *a, size_t n);
size_t str_replace(char *s, int oldc, int newc);
char *str_find_last(const char *s, int c);
int main()
{
int a[] = {1,3,4,5,7,7,3,5,7,4,4,2,3,7};
char b[] = "hello world";
printf("%d\n",*arr_first_max(a, 14));
printf("%d\n", *arr_last_max(a, 14));
printf("%ld\n", str_replace(b, 'l', 'x'));
printf("%c\n", *str_find_last(b, 'e'));
getchar();
return 0;
}
int *arr_first_max(const int *a, size_t n) {
const int *p;
const int *max = a;
for (p = a; p<a + n; p++)
if (p > max)
max = p;
return (int*)max;
}
int *arr_last_max(const int *a, size_t n) {
const int *p;
const int *max = a;
for (p = a; p<a + n; p++)
if (p >= max)
max = p;
return (int*)max;
}
size_t str_replace(char *s, int oldc, int newc) {
size_t rep = 0;
char *p;
for (p = s; *p != '\0'; p++)
if (*p == oldc) {
*p = newc;
rep++;
}
return rep;
}
char *str_find_last(const char *s, int c) {
const char *p;
const char *last = 0;
for (p = s; *p != '\0'; p++)
if (*p == c)
last = p;
return (char*)last;
}