当前位置: 首页 > news >正文

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

文章目录

  • C语言开发,指针进阶。
    • 1.字符串与指针的关系
    • 2.指针获取字符串具体内容
    • 3.字符串比较,查找,包含,拼接
    • 4.字符串大小写

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("----");}//doubledouble 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,进ifif (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;
}
http://www.lryc.cn/news/207326.html

相关文章:

  • PyCharm中文使用详解
  • 一键同步,无处不在的书签体验:探索多电脑Chrome书签同步插件
  • 在Go项目中二次封装Kafka客户端功能
  • CVE-2021-44228 Apache log4j 远程命令执行漏洞
  • 前端跨域相关
  • HTML笔记-狂神
  • python自动化测试工具selenium
  • 输入/输出应用程序接口和设备驱动程序接口
  • Python---Socket 网络通信
  • 使用 jdbc 技术升级水果库存系统(优化版本)
  • 网络协议--广播和多播
  • python爬虫入门(三)正则表达式
  • fabric.js介绍
  • YOLOv5源码中的参数超详细解析(3)— 训练部分(train.py)| 模型训练调参
  • Linux高性能编程学习-TCP/IP协议族
  • 用爬虫代码爬取高音质音频示例
  • 深度学习与计算机视觉(一)
  • 【vector题解】杨辉三角 | 删除有序数组中的重复项 | 只出现一次的数字Ⅱ
  • 金字塔切分注意力模块PSA学习笔记 (附代码)
  • Jenkins自动化测试
  • python 字典dict和列表list的读取速度问题, range合并
  • 测试用例的设计方法(全):等价类划分方法
  • Office技巧(持续更新)(Word、Excel、PPT、PowerPoint、连续引用、标题、模板、论文)
  • Java实现ORM第一个api-FindAll
  • HFSS笔记——求解器和求解分析
  • jenkins配置gitlab凭据
  • 0基础学习PyFlink——用户自定义函数之UDTF
  • 【Java 进阶篇】Java Request 原理详解
  • 13 结构性模式-装饰器模式
  • 支持向量机(SVM)