zoukankan      html  css  js  c++  java
  • 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 }
    注意:不是所有的二级之指针都能当作二维数组来操作
    所有的二维数组都能当作二维指针来使用

    冒泡排序的优化算法

    循环优化
    定义一个int类型 初始化为1;
    在第二层循环的交换语句的开始 将定义的int类型的flag 赋值为零,开始内层循环结束,判断flag是否为零,如果为零直接return,
    否则在外层循环再将flag赋值为1,再次进行内循环,直至执行flag判断的语句;
     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 }

    利用strstr标准库函数找出一个字符串中substr出现的个数。

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include <string.h>
     4 
     5 int main(void)
     6 {
     7     char *p = "11abcd111122abcd333abcd3322abcd3333322qqq";
     8     int n = 0;
     9 
    10     while ((p = strstr(p, "abcd")) != NULL)
    11     {
    12         //能进来,肯定有匹配的子串
    13         //重新设置起点位置
    14         p = p + strlen("abcd");
    15         n++;
    16 
    17         if (*p == 0) //如果到结束符
    18         {
    19             break;
    20         }
    21 
    22     }
    23 
    24     printf("n = %d\n", n);
    25     system("pause");
    26     return 0;
    27 }
     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include <string.h>
     4 
     5 int main(void)
     6 {
     7     char *p = "11abcd111122abcd333abcd3322abcd3333322qqq";
     8     int n = 0;
     9 
    10     do
    11     {
    12         p = strstr(p, "abcd");
    13         if (p != NULL)
    14         {
    15             n++; //累计个数
    16 
    17             //重新设置查找的起点
    18             p = p + strlen("abcd");
    19 
    20         }
    21         else //如果没有匹配的字符串,跳出循环
    22         {
    23             break;
    24         }
    25     } while (*p != 0); //如果没有到结尾
    26 
    27     printf("n = %d\n", n);
    28     return 0;
    29 }

    求非空字符串元素的个数

    两头堵模型

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include <string.h>
     4 #include <ctype.h>
     5 
     6 int fun(char *p, int *n)
     7 {
     8     if (p == NULL || n == NULL)
     9     {
    10         return -1;
    11     }
    12 
    13     int begin = 0;
    14     int end = strlen(p) - 1;
    15 
    16     //从左边开始
    17     //如果当前字符为空,而且没有结束
    18     while (p[begin] == ' ' && p[begin] != 0)
    19     {
    20         begin++; //位置从右移动一位
    21     }
    22 
    23     //从右往左移动
    24     while (p[end] == ' ' && end > 0)
    25     {
    26         end--; //往左移动
    27     }
    28 
    29     if (end == 0)
    30     {
    31         return -2;
    32     }
    33 
    34     //非空元素个数
    35     *n = end - begin + 1;
    36 
    37     return 0;
    38 }
    39 
    40 int main(void)
    41 {
    42     char *p = "      abcddsgadsgefg      ";
    43     int ret = 0;
    44     int n = 0;
    45 
    46     ret = fun(p, &n);
    47     if (ret != 0)
    48     {
    49         return ret;
    50     }
    51     printf("非空字符串元素个数:%d\n", n);
    52 
    53     return 0;
    54 }

    字符串反转模型(逆置)

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include <string.h>
     4 
     5 int inverse(char *p)
     6 {
     7     if (p == NULL)
     8     {
     9         return -1;
    10     }
    11     char *str = p;
    12     int begin = 0;
    13     int end = strlen(str) - 1;
    14     char tmp;
    15 
    16     while (begin < end)
    17     {
    18         //交换元素
    19         tmp = str[begin];
    20         str[begin] = str[end];
    21         str[end] = tmp;
    22 
    23         begin++;  //往右移动位置
    24         end--;        //往左移动位置
    25     }
    26 
    27     return 0;
    28 }
    29 
    30 int main(void)
    31 {
    32     //char *str = "abcdefg"; //文件常量区,内容不允许修改
    33     char str[] = "abcdef";
    34 
    35     int ret = inverse(str);
    36     if (ret != 0)
    37     {
    38         return ret;
    39     }
    40 
    41     printf("str ========== %s\n", str);
    42     return 0;
    43 }

    指针小结

    定义

    说明

    int  i

    定义整形变量

    int *p

    定义一个指向int的指针变量

    int a[10]

    定义一个有10个元素的数组,每个元素类型为int

    int *p[10]

    定义一个有10个元素的数组,每个元素类型为int*

    int func()

    定义一个函数,返回值为int型

    int *func()

    定义一个函数,返回值为int *型

    int **p

    定义一个指向int的指针的指针,二级指针

  • 相关阅读:
    J2EE13个规范--【J2EE】
    事件监听、持有对方的引用--【J2SE】
    TCP协议:服务端和客户端demo--【J2SE】
    线程:Interrupt、Sleep、Join、线程同步--【J2SE】
    1. Visual C++ 6.0 安装和使用
    无法连接虚拟设备sata0:1,因为主机上没有相应的设备
    Linux文件夹文件创建、删除
    服务器
    linux 下 apache启动、停止、重启命令
    Apache部署静态html
  • 原文地址:https://www.cnblogs.com/MetaWang/p/9882799.html
Copyright © 2011-2022 走看看