C语言指针训练

去空字符串

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<stdlib.h>
 5 
 6 char *  removeSpace(char * arr)
 7 {
 8     //char temp[100];
 9     char * start = arr;
10     //字符串有效长度需要-1为数组元素下标
11     char * end = arr + strlen(arr) - 1;
12     while (*end == ' ' && end > start)
13     {
14         end--;
15     }
16     *(end + 1) = '\0';
17     while (*start == ' ' && start < end)
18     {
19         start++;
20     }
21     return start;
22 
23 }
24 
25 int main()
26 {
27 
28     char arr[] = "       你好         ";
29 
30     char * p= removeSpace(arr);
31     printf("%s\n", p);
32 
33 
34     printf("%d\n", sizeof(int **));
35     printf("%d\n", sizeof(int ***));
36     printf("%d\n", sizeof(void *));
37     system("pause");
38     return EXIT_SUCCESS;
39 }

 指针和函数

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<stdlib.h>
 5 
 6 void tab1(int a, int b)
 7 {
 8     int temp = a;
 9     a = b;
10     b = temp;
11     printf("%d    %d\n", a, b);
12     return;
13 }
14 
15 int main1()
16 {
17     int a = 10;
18     int b = 20;
19     tab1(a, b);
20     printf("%d    %d\n", a, b);
21 
22     system("pause");
23     return EXIT_SUCCESS;
24 }
25 void tab(int *a, int *b)
26 {
27     int temp = *a;
28     *a = *b;
29     *b = temp;
30 }
31 int main()
32 {
33 
34     int a = 10;
35     int b = 20;
36     tab(&a, &b);
37     printf("%d   %d\n", a, b);
38 
39     tab1(a, b);
40     system("pause");
41     return 0;
42 }

指针作为函数参数

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<stdlib.h>
 5 
 6 //1、数组作为函数参数可以退化为指针
 7 //2、在传递数组时需要加上数组的个数
 8 
 9 void print01(int * arr, int len)
10 {
11     //函数参数中如有有数组  都会转化为指针  sizeof(int *)  4  所以求出来的值不能作为数组的循环条件存在
12     for (int i = 0; i < len; i++)
13     {
14         printf("%d\n", arr[i]);
15     }
16 }
17 int main2()
18 {
19 
20     int arr[] = { 1,2,3,4,6,0,7,8,9,10 };
21 
22     print01(arr, sizeof(arr) / sizeof(arr[0]));
23     system("pause");
24     return EXIT_SUCCESS;
25 }
26 void print(char * arr)
27 {
28     //两种方式可以求出字符串长度 \0
29     int len = strlen(arr);
30     int i = 0;
31     while (arr[i] != '\0')
32     {
33         i++;
34     }
35     printf("%d\n", i);
36 }
37 int main(void)
38 {
39     char arr[] = "hello world";//字符串
40     //char arr[] = { 'h','e','l','l','o' };//字符数组
41     print(arr);
42     system("pause");
43     return 0;
44 }

函数的返回值是指针

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<stdlib.h>
 5 
 6 int aa = 10;//全局变量
 7 
 8 char * test()
 9 {
10     //字符数组  创建位置在栈区  
11     //char arr[] = "hello world";
12     //字符串常量  会在程序运行时   常量区  不能被修改的 在程序结束时 销毁
13     char * arr = "hello world";
14     //static 
15     aa = 100;
16     //保证指针地址对应的值是有内容的
17     return arr;
18 }
19 int main04()
20 {
21     char * p = test();
22     printf("%p\n", p);
23     printf("%s\n", p);
24 
25     system("pause");
26     return EXIT_SUCCESS;
27 }
28 
29 //strstr   hello  world   llo

实现strstr函数

#include <string.h>

char*strstr(constchar *haystack, constchar *needle);

功能:在字符串haystack中查找字符串needle出现的位置

参数:

       haystack:源字符串首地址

       needle:匹配字符串首地址

返回值:

       成功:返回第一次出现的needle地址

       失败:NULL

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<stdlib.h>
 5 //不是很好
 6 /*
 7 1、两个匹配的字符串 必须完全匹配  匹配个数 = 字符串长度
 8 2、如果配匹一个字符串,需要记录被匹配字符串地址
 9 3、如果匹配一半未成功 回到记录被匹配字符串地址+1
10 4、如果匹配的被匹配字符串的结尾  匹配个数 不等于 字符串长度
11 */
12 
13 char * mystrstr(char * dest, char *src)
14 {
15     int i = 0;
16     int j = 0;
17 
18     //匹配个数
19     int count = 0;
20     int len = strlen(src);
21     char * p = NULL;
22     while (dest[i] != '\0')
23     {
24         //if (dest[i] == src[i]);
25 
26         while (dest[i] == src[j] && dest[i])//匹配个数 = 字符串长度 l l     l o
27         {
28             if (!count)
29                 //如果匹配成功一个字符  需要记录位置
30                 p = &dest[i];
31             count++;
32             i++; 
33             j++;
34             //匹配成功
35             if (count == len)
36             {
37                 return p;
38             }
39 
40         }
41 
42         //发生改变的值  i  j  count  p
43         if (count < len)
44         {
45             i = i - count;
46             j = 0;
47             //count 归 0
48             count = 0;
49             //continue;
50         }
51 
52         i++;
53     }
54 
55     //返回值结果
56     //return p;
57     return NULL;
58 }
59 
60 int main()
61 {
62 
63     char *p = mystrstr("helllllo", "lllllo");
64     printf("%s\n", p);
65 
66     system("pause");
67     return EXIT_SUCCESS;
68 }
 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<stdlib.h>
 5 
 6 /*
 7 1、两个匹配的字符串 必须完全匹配  匹配个数 = 字符串长度
 8 2、如果配匹一个字符串,需要记录被匹配字符串地址
 9 3、如果匹配一半未成功 回到记录被匹配字符串地址+1
10 4、如果匹配的被匹配字符串的结尾  匹配个数 不等于 字符串长度
11 */
12 
13 char * mystrstr(char * dest, char *src)
14 {
15     char * p = NULL;
16     char * temp = src;
17     while (*dest)//
18     {
19         p = dest;
20         while (*dest == *temp && *dest)//匹配个数 = 字符串长度 l l     l o
21         {
22             dest++;
23             temp++;
24         }
25         if (!*temp)//\0
26                    //if (*temp=='\0')//\0
27             return p;
28         else
29             temp = src;
30         dest = p;
31         dest++;
32     }
33 
34     //返回值结果
35     //return p;
36     return NULL;
37 }
38 
39 int main()
40 {
41 
42     char *p = mystrstr("helllo", "lll");
43     printf("%s\n", p);
44 
45     system("pause");
46     return EXIT_SUCCESS;
47 }

指针和字符串

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<stdlib.h>
 5 
 6 int main1()
 7 {
 8 
 9     char arr[] = "hello world";//ABllo world
10     char * p;
11     p = arr;
12     *p = 'A';//arr[0] p[0]
13 
14     p++;//arr[1] p[1]
15     *p = 'B';
16     printf("%s\n", arr);
17     printf("%d\n", sizeof(arr));//12
18     printf("%d\n", strlen(arr));//11
19     printf("%d\n", sizeof(p));//4
20     printf("%d\n", strlen(p));//1 4 10
21 
22     system("pause");
23     return EXIT_SUCCESS;
24 }
25 
26 int main()
27 {
28     char * arr = "hello world";//常量区
29     char  arr1[] = "hello world";//栈区
30     printf("%s\n", arr);
31     printf("%c\n", arr[0]);
32     char * p = arr;
33     printf("%p\n", p);
34     //字符串常量是一个常量的数组 可以读取字符或者字符串  但是不能修改
35     //p[0] = 'A';
36     //*p = 'A';
37     p = arr1;
38     printf("%p\n", p);
39 
40     //p[0] = 'A';
41     //*p = 'A';
42     //printf("%s", p);
43     system("pause");
44     return EXIT_SUCCESS;
45 }

实现strcat函数

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<stdlib.h>
 5 
 6 void mystrcat(char * arr, char * s1)
 7 {
 8     //while (*arr)
 9     //    arr++;
10     //while (*arr++ = *s1++);
11     while (*arr)
12         arr++;
13     while (*s1)
14     {
15         *arr = *s1;
16         arr++;
17         s1++;
18     }
19     *arr = '\0';
20 }
21 int main()
22 {
23     char arr[ ] = "hello";
24     char * s1 = "world";
25     mystrcat(arr, s1);
26     printf("%s\n", arr);
27 
28 
29 
30     system("pause");
31     return EXIT_SUCCESS;
32 }

字符串排序

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<stdlib.h>
 5 
 6 
 7 //字符串排序  根据字符串首字符 按照a-z的顺序排序
 8 //student tree new bee  bee new student tree
 9 
10 void bubble(char ** arr,int len)
11 {
12     for (int i = 0; i < len - 1; i++)
13     {
14         for (int j = 0; j < len - i - 1; j++)
15         {
16             //比对两个字符串的首字母
17             //1、指针判断
18             //if (**(arr + j) < **(arr + j + 1))
19             //{
20             //    char * temp = *(arr+j);
21             //    *(arr + j) = *(arr + j + 1);
22             //    *(arr + j + 1) = temp;
23             //}
24             //2、数组判断
25             //if (arr[j][0] > arr[j+1][0])
26             //{
27             //    char * temp = arr[j];
28             //    arr[j] = arr[j+1];
29             //    arr[j + 1] = temp;
30             //}
31             //3、混合判断
32             if (*arr[j] > *arr[j + 1])
33             {
34                 char * temp = arr[j];
35                 arr[j] = arr[j+1];
36                 arr[j + 1] = temp;
37             }
38         }
39     }
40 }
41 
42 int main()
43 {
44     char *arr[] = { "cshdf", "ehsdhf", "bjhdjfhd","abee" };
45 
46     /*arr[0][0]
47     student //arr[0]
48     tree//arr[1]
49     new
50     bee
51     */
52     bubble(arr, 4);
53 
54     for (int i = 0; i < 4; i++)
55     {
56         printf("%s\n", arr[i]);
57     }
58     //printf("%c\n", arr[0][0]);
59     //printf("%c\n", arr[1][0]);
60     //printf("%c\n", arr[2][0]);
61     //printf("%c\n", arr[3][0]);
62 
63 
64     system("pause");
65     return EXIT_SUCCESS;
66 }

冒泡排序的优化算法

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<stdlib.h>
 5 
 6 void bubble(int *arr, int len)
 7 {
 8     int flag = 1;
 9     for (int i = 0; i < len - 1; i++)
10     {
11         for (int j = 0; j < len - i - 1; j++)
12         {
13             if (arr[j] < arr[j + 1])
14             {
15                 flag = 0;
16                 int temp = arr[j];
17                 arr[j] = arr[j + 1];
18                 arr[j + 1] = temp;
19             }
20         }
21         if (flag)
22             return;
23         flag = 1;
24     }
25 }
26 int main()
27 {
28     int arr[] = { 1,3,5,8,9,2,7,4,6,0 };
29     bubble(arr, 10);
30 
31     for (int i = 0; i < 10; i++)
32     {
33         printf("%d\n", arr[i]);
34     }
35     system("pause");
36     return EXIT_SUCCESS;
37 }

猜你喜欢

转载自www.cnblogs.com/WangGuiHandsome/p/9882799.html