• C语言开发,指针进阶,字符串查找,包含,拼接


    C语言开发,指针进阶。

    1.字符串与指针的关系

    //
    // Created by MagicBook on 2023-10-22.
    //
    #include 
    
    int main11() {
        char str[] = {'d', '1', 's','\0'};
        //str数组的字符串是存在全局区,静态区域,修改的时候拷贝一份到main函数的栈区,再进行修改操作
        str[1] = 'a';
        //printf遇到\0结束
        printf("%s\n", str);
    
        //开辟内存地址,指向静态区域里面的aaaa,指针不能修改静态全局区域内的字符串内容,
        char *str2 = "aaaa";
        //崩溃
        str2[1] = '2';
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    2.指针获取字符串具体内容

    //
    // Created by MagicBook on 2023-10-22.
    //
    #include 
    
    int main11() {
        char str[] = {'d', '1', 's','\0'};
        //str数组的字符串是存在全局区,静态区域,修改的时候拷贝一份到main函数的栈区,再进行修改操作
        str[1] = 'a';
        //printf遇到\0结束
        printf("%s\n", str);
    
        //开辟内存地址,指向静态区域里面的aaaa,指针不能修改静态全局区域内的字符串内容,
        char *str2 = "aaaa";
        //崩溃
        str2[1] = '2';
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    //
    // Created by MagicBook on 2023-10-22.
    //
    #include 
    
    int getLen(char *string) {
        int cnt = 0;
        //移动指针 ,不等于'\0',一直循环,
        while (*string) {
            string++;
            cnt++;
        }
        return cnt;
    }
    
    //数组作为参数传递,会把数组优化成指针
    void getLen2(int *res, int arr[]) {
        /* int len = sizeof(arr) / sizeof(int);
         printf("%d\n", len);*/
        int cnt = 0;
        while (*arr) {
            arr++;
            cnt++;
        }
        *res = cnt;
    }
    
    int main() {
        char str[] = {'d', '1', 's', '\0'};
        int len = getLen(str);
        printf("%d\n", len);
        int arr[] = {1, 2, 3, '\0'};
        int len2 = sizeof(arr) / sizeof(int);
        int res;
        getLen2(&res, arr);
        printf("%d\n", len2);
    
        return 0;
    }
    
    • 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

    3.字符串比较,查找,包含,拼接

    //
    // Created by MagicBook on 2023-10-22.
    //
    #include 
    #include 
    #include 
    
    int main() {
        //字符串转换
        char *aaa = "1222";
        int res = atoi(aaa);
        //true,不等于0,
        if (res) {
            printf("%d\n", res);
        } else {
            printf("----");
        }
        //double
        double res2 = atof(aaa);
        printf("%lf\n", res);
    
        //字符串比较
        char *string1 = "aaa";
        char *string2 = "aaasss";
        //区分大小写
        int ress = strcmp(string1, string2);
        //不区分大小写
        int resss = stricmp(string1, string2);
        //0是相等,非0不相等
        if (!ress) {
            printf("yes");
        } else {
            printf("no");
        }
        return 0;
    }
    
    • 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
    //
    // Created by MagicBook on 2023-10-22.
    //
    #include 
    #include 
    #include 
    
    int main() {
        char *aa = "1234";
        char *aaa = "1";
    
        char *test = strstr(aa, aaa);
        //非NULL,进if
        if (test) {
            printf("%s\n", test);
        } else {
    
        }
        //包含
        if (test) {
    
        } else {
    
        }
        //取位置
        int index = test - aa;
    
        //拼接字符串
        char test1[22];
        char *a1 = "11", *a2 = "22", *a3 = "33";
        //先拷贝到数组容器中,再拼接字符串
        strcpy(test1, a1);
        strcat(test1, a2);
        strcat(test1, a3);
        return 0;
    }
    
    • 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

    4.字符串大小写

    //
    // Created by MagicBook on 2023-10-22.
    //
    #include 
    #include 
    #include 
    #include 
    
    void low(char *b, char *c) {
        while (*c) {
            *b = tolower(*c);
            //移动指针
            c++;
            //一边移动一边存储。
            b++;
        }
        *b = '\0';
    }
    
    int main() {
        //都变成小写
        char *a = "qDHaAA";
        //
        char test[20];
        low(test, a);
        return 0;
    }
    
    • 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
  • 相关阅读:
    可以一键生成热点营销视频的工具,建议收藏
    ES证书过期替换方案
    同事提出个我从未想过的问题,为什么Kubernetes要"多此一举"推出静态Pod概念?
    解读PowerJob的秘密武器:探索Akka Toolkit原理
    设计模式_迭代器模式
    @Accessors 注解详解
    ChatGPT调教指南 | 咒语指南 | Prompts提示词教程(二)
    神经网络编程的34个案例,神经网络编程是什么
    android源码学习-Toast实现原理讲解
    使用51单片机控制lcd1602字体显示
  • 原文地址:https://blog.csdn.net/weixin_46039528/article/details/134001704