选择排序
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <time.h>
4
5 int main(void)
6 {
7 int a[6] = {0};
8 int i = 0;
9 int j = 0;
10 int min = 0;
11 int temp = 0;
12 int len = sizeof(a) / sizeof(a[0]);
13
14 srand(time(NULL));
15 printf("Before : ");
16 for (i = 0; i < len; i++)
17 {
18 a[i] = rand() % 100;
19 printf("%d ", a[i]);
20 }
21 printf("\n");
22
23 for (j = 0; j < len - 1; j++)
24 {
25 min = j;
26 for (i = j + 1; i < len; i++)
27 {
28 if (a[i] < a[min])
29 {
30 min = i;
31 }
32 }
33
34 if (min != j)
35 {
36 temp = a[min];
37 a[min] = a[j];
38 a[j] = temp;
39 }
40 }
41
42 printf("After : ");
43 for (i = 0; i < len; i++)
44 {
45 printf("%d ", a[i]);
46 }
47 printf("\n");
48
49 return 0;
50 }
计算输入字符串中大写字母,小写字母,空格,数字的个数
1 #include <stdio.h>
2
3 int main(void)
4 {
5 char str[64] = {0};
6 int i = 0;
7 int j = 0;
8 int n = 0;
9 int m = 0;
10 int k = 0;
11
12 gets(str);
13
14 while (str[i] != '\0')
15 {
16
17 if ('A' <= str[i] && 'Z' >= str[i])
18 {
19 k++;
20 }
21 else if ('a' <= str[i] && 'z' >= str[i])
22 {
23 j++;
24 }
25 else if (str[i] == ' ')
26 {
27 n++;
28 }
29 else if ('0' <= str[i] && '9' >= str[i])
30 {
31 m++;
32 }
33 i++;
34 }
35
36 printf("k = %d\nj = %d\nn = %d\nm = %d\n", k, j, n, m);
37
38 return 0;
39 }
strlen,strcpy,strcmp,strcat练习
1 #include <stdio.h>
2 #include <string.h>
3
4 int main(void)
5 {
6 char str[64] = {0};
7 char dst[64] = {0};
8 int ret = 0;
9 int len = 0;
10
11 gets(str);
12 gets(dst);
13 //strcpy(str, "Thank");
14 //strcpy(dst, "you");
15 //strcat(str, dst);
16
17 len = strlen(str);
18
19 ret = strcmp(str, dst);
20
21 printf("len = %d\n", len);
22 printf("ret = %d\n", ret);
23
24 if (!strcmp(str, dst))
25 {
26 printf("Character sting is same\n");
27 }
28 if (0 < ret)
29 {
30 printf("str > dst\n");
31 }
32 if (0 > ret)
33 {
34 printf("str < dst\n");
35 }
36 //puts(str);
37
38 return 0;
39 }