指针实战运用进阶之字符串操作

文章目录

字符串两种形式

c 复制代码
#include <stdio.h>

// 字符串
int mainT2() {
    char str[] = {'D', 'e', 'r', 'r', 'y', '\0'};
    str[2] = 'z'; // 这里能修改
    printf("第一种方式:%s\n", str); // printf 必须遇到 \0 才结束

    char * str2 = "Derry"; 
    str2[2] = 'z'; // 会奔溃,不允许访问
    printf("第二种方式:%s\n", str);

    return 0;
}

指针挪动获取字符串信息(手写API)

c 复制代码
#include<stdio.h>
int getLen(char* string) {
	int count = 0;
	while (*string) { //*string相当于*string!='\0'我就一直循环
		string++;
		count++;
	}
	return count;
}


int main() {
	char str[] = {'C','h','e','n','\0'};

	int r = getLen(str);
	printf("长度是:%d\n", r);


	return 0;
}
bash 复制代码
长度是:4
c 复制代码
#include<stdio.h>



void getLen(int* resultLen, int intarr[]) {

	//int len = sizeof(intarr) / sizeof(int);
	//printf("getLen Len长度是:%d\n", len);
	int count = 0;
	while (*intarr != '\0') {
		intarr++;
		count++;
	}
	*resultLen = count;
}

int main() {

	int intarr[] = { 1,2,3,4,5,6,7,'\0'};
	printf("长度%d %d\n", sizeof(intarr), sizeof(int));
	int result;//&取出result遍历的地址给函数
	getLen(&result, intarr);
	printf("getLen Len长度是:%d\n", result);

	return 0;
}
bash 复制代码
长度32 4
getLen Len长度是:7

字符串的比较

c 复制代码
// 4.字符串转换,字符串的比较。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// 快捷键:按两下 shift键  选择 file encoding

// 真正的C语言,在Linux上学,在VIM上才是专业的
int main() {
    // 字符串转换 =======================
    char * num = "1"; // 字符串
    num = "12.68";

    // 【int】
    int result =  atoi(num);
    if (result) { // 非0即ture  不是0进入if,  0就是转换失败了
        printf("恭喜你转换成功:%d\n", result);
    } else {
        printf("转换失败!\n");
    }

    // 【double】
    double resultD =  atof(num);
    printf("恭喜你转换成功:%lf\n", resultD);


    // 字符串的比较 ======================
    char * str1 = "ChenYiRan";
    char * str2 = "chenyiran";

    // int resultC = strcmp(str1, str2); // strcmp = 区分大小写
    int resultC = strcmpi(str1, str2); // strcmpi = 不区分大小写

    if (!resultC) { // 0代表是相等的, 非0代表是不相等的
        printf("相等");
    } else {
        printf("不相等");
    }

    return 0;
}

字符串查找,包含,拼接

c 复制代码
// 字符串查找,包含,拼接。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int mainT5() {

    char * text = "name is chenyiran";
    char * subtext = "chenyiran";

    char * pop = strstr(text, subtext);

    // 怎么去 字符串查找
    if (pop) { // 非NULL,就进入if,就查找到了
        printf("查找到了,pop的值是:%s\n", pop);
    } else {
        printf("没有查找到,subtext的值是:%s\n", subtext);
    }

    // 包含了D吗
    if (pop) {
        printf("包含了\n");
    } else {
        printf("没有包含\n");
    }

    // printf("pop地址%p, text地址:%p,\n", pop, text);

    // 求取位置?  数组是一块连续的内存空间,没有断层,所以可以-
    int index = pop - text; // pop="chenyiran" - text"name is chenyiran"
    printf("%s第一次出现的位置是:%d\n", subtext, index); // text指向n,pop指向c,两者之间相差8个字节

    // 指针是可以:++ --  +=  -=

    // 拼接 ========================
    char destination[25]; // 容器 25的大小 已经写死了
    char * blank = "--到--", *CPP="C++", *Java= "Java";

    strcpy(destination, CPP); // 先Copy到数组里面去
    strcat(destination, blank); // 然后再拼接
    strcat(destination, Java); // 然后再拼接
    printf("拼接后的结果:%s\n", destination); // C++--到--Java

    return 0;
}

大小写转换(手写API)

c 复制代码
// 大小写转换(手写API)

#include <stdio.h>
#include <ctype.h>

// 指针的理解
void lower(char * dest, char * name) {
    char * temp = name; // 临时指针,你只能操作,临时指针,不能破坏name指针
    while (*temp) {
        *dest = tolower(*temp);
        temp ++; // 挪动指针位置 ++
        dest ++; // 挪动指针位置 ++  目的是为了 挪动一个存储一个 挪动一个存储一个 ...
    }
    // printf '\0'
    *dest = '\0'; // 避免printf打印系统值

    printf("不能破坏 name:%s\n", name); // temp的好处就是,不会破坏name
}

// 全部变成小写 chenyiran
int mainT6() {
    char * name = "ChenYiRan";

    // 先定义结果
    char dest[20];
    lower(dest, name);
    printf("小写转换后的结构是:%s\n", dest);

    return 0;
}
c 复制代码
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

// TODO 第一版   通用
void substrAction1(char * result, char * str, int start, int end) {
    char * temp = str; // 定义临时指针,不破坏str指针
    int count = 0; // 记录当前的位置
    while (*temp) {
        // 2 到 5 的位置  在截取的范围内
        if (count >= start && count < end) {
            *result = *temp; 
            result++; // (接收值也要挪动)挪动指针来接收   =   *temp给我的值
        }
        temp++; // 取值也要挪动
        count++; // 当前的位置要同步
    }
}

// TODO 第二版    有意让同学,深刻理解  栈区 堆区 开辟(1)
void substrAction2(char ** result, char * str, int start, int end) {
    char * temp = str; // 定义临时指针,不破坏str指针

    // 合理分配,截取多少用多少,节约
    char resultArr[end - start]; // 我只需要你截取的大小空间

    // 尽量不要使用第二种方式,会被C工程师鄙视的,为什么? 你开辟的,就应该你回收
    // char * resultArr = malloc(end - start); // 堆区开辟,第二种解决方案

    int count = 0;
    for (int i = start; i < end; ++i) {
        resultArr[count] = *(temp + i); // *(temp + i);取出D e r r y 给  数组容器
        count++;
    }

    // * 取出二级指针的一级指针 ==  main函数的result一级指针

    // *result = resultArr; // 不能让我们的一级指针 指向容器,因为容器会被回收掉

    strcpy(*result, resultArr); // 第一种解决方案

    printf("%s\n", resultArr);

    // 1   2  不能回收堆空间,否则main函数 打印了空
    // free(resultArr);

} // 函数弹栈后,会回收所有的栈成员,包括:resultArr

// TODO 第三版    三行代码搞定
void substrAction3(char * result, char * str, int start, int end) { // 没有涉及栈区 堆区的概念
    // 合理分配,截取多少用多少,节约 思路
    for (int i = start; i < end; ++i) { // 刚好结束 循环三次
        *(result++) = *(str+i); // i = 2
    }
}

// TODO 第四版   一行代码搞定
void substrAction4(char * result, char * str, int start, int end) {
    // 参数1:我最终是copy到result容器里面
    // 参数2:直接从r开始,因为我一级做了,指针挪动了
    // 参数3:你从r开始,挪动多少
    strncpy(result, str+start, end - start);
}

// 【截取】字符串的截取操作
int main() {

    char *str = "Chen is";
    // 正好她是一级指针
    char *result; // char * 不需要结尾符\0

    // 截取第二个位置到第五个位置 2,5

    // substrAction1(result, str, 2, 5);
    // substrAction2(&result,str, 2, 5);
    // substrAction3(result,str, 2, 5);
    substrAction4(result, str, 2, 5);

    printf("main 截取的内容是:%s", result); 

    if (result) {
        free(result);
        result = NULL;
    }

    return 0;
}
相关推荐
Mr YiRan3 小时前
静态开辟内存与动态开辟内存
c语言
935964 小时前
机考24 翻译18 单词11
c语言·数据结构·算法
民国二十三画生5 小时前
C++(兼容 C 语言) 的标准输入语法,用来读取一行文本
c语言·开发语言·c++
学编程的闹钟7 小时前
C语言GetLastError函数
c语言·开发语言·学习
IvanCodes8 小时前
七、C语言指针
c语言·开发语言
学编程的闹钟9 小时前
C语言WSAGetLastError函数
c语言·开发语言·学习
wengqidaifeng9 小时前
数据结构---链表的奇特(下)双向链表的多样魅力
c语言·数据结构·链表
承渊政道10 小时前
C++学习之旅【C++继承概念指南与核心内容介绍】
c语言·开发语言·c++·笔记·学习·visual studio
小龙报11 小时前
【51单片机】51 单片机 IIC 协议深度解析:时序实现 + GXHT3L 连续转换模式 + 数据解析
c语言·数据结构·stm32·单片机·嵌入式硬件·物联网·51单片机