当前位置:网站首页>字符串
字符串
2022-06-25 21:59:00 【打酱油的;】
1. 字符串概念以及基本操作
字符串是以0或者'\0'结尾的字符数组,(数字0和字符'\0'等价),字符数组只能初始化5个字符,当输出的时候,从开始位置直到找到0结束
字符数组部分初始化,剩余填0
如果以字符串初始化,那么编译器默认会在字符串尾部添加'\0'
说明:
sizeof计算数组大小,数组包含'\0'字符
strlen计算字符串的长度,到'\0'结束
示例代码:
//字符串基本操作 //字符串是以0或者'\0'结尾的字符数组,(数字0和字符'\0'等价) void test01(){ //字符数组只能初始化5个字符,当输出的时候,从开始位置直到找到0结束 char str1[] = { 'h', 'e', 'l', 'l', 'o' };出现乱码!因为找不到\0 printf("%s\n",str1); //字符数组部分初始化,剩余填0 char str2[100] = { 'h', 'e', 'l', 'l', 'o' };不会出现乱码,因为其余位置自动补0 printf("%s\n", str2); //如果以字符串初始化,那么编译器默认会在字符串尾部添加'\0' char str3[] = "hello";自带\0 printf("%s\n",str3); printf("sizeof str:%d\n",sizeof(str3));6 printf("strlen str:%d\n",strlen(str3));5 //sizeof计算数组大小,数组包含'\0'字符 //strlen计算字符串的长度,到'\0'结束 //那么如果我这么写,结果是多少呢? char str4[100] = "hello"; printf("sizeof str:%d\n", sizeof(str4));100 printf("strlen str:%d\n", strlen(str4));5 //请问下面输入结果是多少?sizeof结果是多少?strlen结果是多少? char str5[] = "hello\0world"; printf("%s\n",str5);hello printf("sizeof str5:%d\n",sizeof(str5));12 printf("strlen str5:%d\n",strlen(str5));5 //再请问下面输入结果是多少?sizeof结果是多少?strlen结果是多少? char str6[] = "hello\012world";\012算一个 printf("%s\n", str6);hello 换行 world printf("sizeof str6:%d\n", sizeof(str6));12 printf("strlen str6:%d\n", strlen(str6));11 } |
八进制和十六进制转义字符:
在C中有两种特殊的字符,八进制转义字符和十六进制转义字符,八进制字符的一般形式是'\ddd',d是0-7的数字。十六进制字符的一般形式是'\xhh',h是0-9或A-F内的一个。八进制字符和十六进制字符表示的是字符的ASCII码对应的数值。转为10进制对于ascll表字符 比如 :
|

2.字符串拷贝功能实现操作
前提都要先初始化一下,不要是野指针 //拷贝方法1---利用数组下标拷贝(其实也是指针)函数内的数组下标相当于*(p+i) void copy_string01(char* dest, char* source ){//将内存一个个取出来,放入另一个,到结束符为止 for (int i = 0; source[i] != '\0';i++){ dest[i] = source[i]; } } //拷贝方法2---利用指针进行拷贝 void copy_string02(char* dest, char* source){ while (*source != '\0' /* *source != 0 */){遍历一整个字符串,俩个首地址一个个按步长增加,然后赋值 *dest = *source; source++; dest++; } } //拷贝方法3(较难) void copy_string03(char* dest, char* source){ //判断*dest是否为0,0则退出循环 while (*dest++ = *source++){} } |

3. 字符串反转模型操作

方法1:利用下标的方法 void reverse_string(char* str){ if (str == NULL){ return; } int begin = 0; int end = strlen(str) - 1; while (begin < end){判断当头坐标大于尾坐标时,不能相等,因为偶数永远不会相等 //交换两个字符元素 char temp = str[begin]; str[begin] = str[end]; str[end] = temp; begin++; end--; } } void test(){ char str[] = "abcdefghijklmn"; printf("str:%s\n", str); reverse_string(str); printf("str:%s\n", str); } |
方法2:利用字符串指针

4.字符串的格式化
sprintf
#include <stdio.h> int sprintf(char *str, const char *format, ...); 功能: 根据参数format字符串来转换并格式化数据,然后将结果输出到str指定的空间中,直到 出现字符串结束符 '\0' 为止。 参数: str:字符串首地址,格式化后往地址内填充字符数据类型要是开辟好的char形 format:字符串格式,用法和printf()一样 ...:格式化中的参数(可多位) 返回值: 成功:实际格式化的字符个数(strlen的结果) 失败: - 1 |
格式化类型:
void test(){ //1. 格式化字符串(常用) 最好不能是空指针或者是野指针 char buf[1024] = { 0 }; sprintf(buf, "你好,%s,欢迎加入我们!", "John"); printf("buf:%s\n",buf); memset(buf, 0, 1024); sprintf(buf, "我今年%d岁了!", 20); printf("buf:%s\n", buf); //2. 拼接字符串 memset(buf, 0, 1024); char str1[] = "hello"; char str2[] = "world"; int len = sprintf(buf,"%s %s",str1,str2); printf("buf:%s len:%d\n", buf,len); //3. 数字转字符串格式化输出 memset(buf, 0, 1024); int num = 100; sprintf(buf, "%d", num); printf("buf:%s\n", buf); //设置宽度 右对齐 memset(buf, 0, 1024); sprintf(buf, "%8d", num);一共宽度是8,右对齐 printf("buf:%s\n", buf); //设置宽度 左对齐 memset(buf, 0, 1024); sprintf(buf, "%-8d", num); printf("buf:%s\n", buf); //转成16进制字符串 小写 memset(buf, 0, 1024); sprintf(buf, "0x%x", num); printf("buf:%s\n", buf); //转成8进制字符串 memset(buf, 0, 1024); sprintf(buf, "0%o", num); printf("buf:%s\n", buf); } |
边栏推荐
- How to use JMeter for interface testing
- Pycharm student's qualification expires, prompting no suitable licenses associated with account solution
- Flex & Bison Start
- Multithreaded learning 1
- ES6-Const常量与数组解构
- Multi modal data can also be Mae? Berkeley & Google proposed m3ae to conduct Mae on image and text data! The optimal masking rate can reach 75%, significantly higher than 15% of Bert
- Core points of assembly language
- The wisdom of questioning? How to ask questions?
- Which PHP open source works deserve attention
- Pit resolution encountered using East OCR (compile LAMS)
猜你喜欢

ES6-Const常量与数组解构

UE4_UE5結合offline voice recognition插件做語音識別功能

Multi modal data can also be Mae? Berkeley & Google proposed m3ae to conduct Mae on image and text data! The optimal masking rate can reach 75%, significantly higher than 15% of Bert

Fegin client entry test

Several optimization scenarios using like fuzzy retrieval in SQL

干货丨产品的可行性分析要从哪几个方面入手?

Fastjson deserialization randomness failed

一位博士在华为的22年

ES6 --- 数值扩展、对象拓展

Oracle - getting started
随机推荐
How to download the software package of CDH version
Leetcode(435)——无重叠区间
Huawei cloud SRE deterministic operation and maintenance special issue (the first issue)
. SQL database import error: / *! 40101 SET @OLD_ COLLATION_ [email protected]@COLLATION_ CONNECTION */
Idea auto generator generates constructor get/set methods, etc
一位博士在华为的22年
UE4_UE5結合offline voice recognition插件做語音識別功能
Ue4 Ue5 combine le plug - in de reconnaissance vocale de bureau pour la reconnaissance vocale
NLP pre training model-2018:bert dictionary
Xampp重启后,MySQL服务就启动不了。
提问的智慧?如何提问?
The sum of logarithms in group 52--e of Niuke Xiaobai monthly race (two points)
Technology blog site collection
What is Unified Extensible Firmware Interface (UEFI)?
STM32开发板+机智云AIoT+家庭监测控制系统
关闭MongoDB一些服务需要注意的地方(以及开启的相关命令)
做接口测试,这3种工具到底什么时候用?
How to add cartoon characters to the blog park?
Multithreaded learning 2- call control
C language and the creation and use of database