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

C语言开发,指针进阶。

1.字符串与指针的关系

//
// Created by MagicBook on 2023-10-22.
//
#include <stdio.h>

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;
}

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

//
// Created by MagicBook on 2023-10-22.
//
#include <stdio.h>

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;
}
//
// Created by MagicBook on 2023-10-22.
//
#include <stdio.h>

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;
}

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

//
// Created by MagicBook on 2023-10-22.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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;
}
//
// Created by MagicBook on 2023-10-22.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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;
}

4.字符串大小写

//
// Created by MagicBook on 2023-10-22.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值