网页制作三剑客是指,湖南seo网站设计,初学者做电商怎么入手,企业网站功能需求文档C语言中常用的字符串函数 文章目录 C语言中常用的字符串函数1 strlen函数2 sizeof函数2.1 sizeof介绍2.2 sizeof用法 3 sscanf函数3.1 sscanf介绍3.2 sscanf用法3.3 sscanf高级用法 4 sprintf函数4.1 背景4.2 sprintf用法 5 strcpy函数5.1 strcpy介绍5.1 strcpy用法 1 strlen函…C语言中常用的字符串函数 文章目录 C语言中常用的字符串函数1 strlen函数2 sizeof函数2.1 sizeof介绍2.2 sizeof用法 3 sscanf函数3.1 sscanf介绍3.2 sscanf用法3.3 sscanf高级用法 4 sprintf函数4.1 背景4.2 sprintf用法 5 strcpy函数5.1 strcpy介绍5.1 strcpy用法 1 strlen函数 strlen即为string length顾名思义该函数是用来求字符串长度的。在介绍strlen函数之前我们要先介绍一下\0这个转义字符。任何一个字符串后面都会隐藏一个\0,该转义字符是字符串结束的标志所以我们在使用strlen函数求字符串长度时遇到\0时停止读取此时\0前的字符个数就是字符串的长度。
注意 这里的\0只是结束标志并不算一个字符
示例1
#include stdio.h
#include string.h
#include stdlib.hint main()
{printf(%d\n, strlen(xxccyy));system(pause);return 0;
}我们将字符串xxccyy在内存的存储结构展示如下
示例2 在c语言中字符串并没有作为一种独立的数据类型进行定义。相反字符串被表示为字符数组或字符指针。以下是两种常见的表示字符串的方法
使用字符数组
char str[20] Hello, World!; // 声明一个字符数组来存储字符串使用字符指针
char *str Hello, World!; // 声明一个指向字符的指针指向字符串常量接下来我们介绍使用字符数组存储字符串的每一个字符使用这种定义方式对于strlen的求解有何不同呢
#include stdio.h
#include string.h
#include stdlib.hint main()
{char arr1[] {x,x,c,c,y,y};char arr2[] {x,x,c,c,y,y,\0};printf(%d\n, strlen(arr1));printf(%d\n, strlen(arr2));system(pause);return 0;
}arr1数组只是单纯把字符串“abcdef”的每一个字符用数组存储起来而arr2数组则是多存储了一个“\0可以看到arr1数组的长度为9arr2数组的长度为6接下来我们将展示两个数组在内存中的存储状态。 arr2数组的存储情况和示例1字符串的存储情况相同而arr1却不同。对于arr2我们不进行说明接下来我们分析下为什么arr1数组的长度为9。 上文我们说过字符串结束标志为\0,但是我们的arr1数组没有额外存储\0所以编译器在读取时并不会像我们所期望的那样停止读取故长度当然不会为6。但是为什么最终读取的长度为9是因为在读取时编译器读取完arr1时会继续往后读取直到读取到”\0arr1在读取完第9个字符后才会遇到”\0由于每个人的电脑和编译器不同读取的长度也不一样所以arr1这种情况一般我们认为它读取的结果为随机值
示例3
#include stdio.h
#include string.h
#include stdlib.hint main()
{printf(%d\n, strlen(xcy\0zfr));system(pause);return 0;
}示例3是为了进一步说明字符串结束标志\0的重要性。
2 sizeof函数
2.1 sizeof介绍 sizeof是计算变量在内存的占空间的大小单位是字节。
2.2 sizeof用法
使用sizeof查看数据类型占空间大小
#include stdio.h
#include string.h
#include stdlib.hint main()
{printf(sizeof(char): %d\n, sizeof(char));printf(sizeof(short): %d\n, sizeof(short));printf(sizeof(int): %d\n, sizeof(int));printf(sizeof(long): %d\n, sizeof(long));printf(sizeof(long long): %d\n, sizeof(long long));printf(sizeof(float): %d\n, sizeof(float));printf(sizeof(double): %d\n, sizeof(double));system(pause);return 0;
}使用sizeof计算基本数据类型变量的占用空间的大小
#include stdio.h
#include string.h
#include stdlib.hint main()
{char c a;int i 1;short s 1;long l 1;long long ll 1;float f 1.0;double d 1.0;printf(sizeof(c): %d\n, sizeof(c));printf(sizeof(s): %d\n, sizeof(s));printf(sizeof(i): %d\n, sizeof(i));printf(sizeof(l): %d\n, sizeof(l));printf(sizeof(ll): %d\n, sizeof(ll));printf(sizeof(f): %d\n, sizeof(f));printf(sizeof(d): %d\n, sizeof(d));system(pause);return 0;
}使用sizeof计算指针的占用空间大小 需要注意的是32位平台所有类型的指针的占用空间大小都是4个字节64位平台所有类型的指针占用的空间大小为8个字节观察如下代码
#include stdio.h
#include stdlib.hint main()
{printf(sizeof(char*): %d\n, sizeof(char*));printf(sizeof(short*): %d\n, sizeof(short*));printf(sizeof(int*): %d\n, sizeof(int*));printf(sizeof(long*): %d\n, sizeof(long*));printf(sizeof(long long*): %d\n, sizeof(long long*));printf(sizeof(float*): %d\n, sizeof(float*));printf(sizeof(double*): %d\n, sizeof(double*));system(pause);return 0;
}计算数组元素的个数
如想得到数组的元素个数有以下两种方法 1.总长度/相对应的数据类型长度 2.总长度/首元素长度
#include stdio.h
#include stdlib.hint a[]{1,2,3,4,5};
short b[]{1,2,3,4,5};
long c[]{1,2,3,4,5};
float d[]{1,2,3,4,5};
double e[]{1,2,3,4,5};
char f[]12345;int main(void)
{printf(a%d,b%d,c%d,d%d,e%d,f%d\n,sizeof(a)/sizeof(int), sizeof(b)/sizeof(short), sizeof(c)/sizeof(long),sizeof(d)/sizeof(float),sizeof(e)/sizeof(double),sizeof(f)/sizeof(char));printf(a%d,b%d,c%d,d%d,e%d,f%d\n,sizeof(a)/sizeof(a[0]), sizeof(b)/sizeof(b[0]), sizeof(c)/sizeof(c[0]),sizeof(d)/sizeof(d[0]),sizeof(e)/sizeof(e[0]),sizeof(f)/sizeof(f[0]));system(pause);return 0;
}3 sscanf函数
3.1 sscanf介绍
sscanf函数是C语言中的一个标准库函数用于从格式化的字符串中读取输入。
sscanf的函数原型
#include stdio.h
int sscanf(const char *str, const char *format, ...);其中str表示要读取的字符串format表示格式控制字符串…表示可变参数列表。
3.2 sscanf用法
整形数据转换
#include stdio.h
#include string.h
#include stdlib.hint main()
{int year, month, day;int converted sscanf(20231215, %04d%02d%02d, year, month, day);printf(converted%d, year%d, month%d, day%d\n, converted, year, month, day);system(pause);return 0;
}“%04d%02d%02d是用来解析字符串的格式%表示格式转换的开始d表示转换为一个整数04作为d的修饰表示这是一个长度为4位的整数不足4位时以0补齐。 返回值converted等于3表示有3个数据成功转换转换成功数目同时取决于被解析的字符串以及其转换格式如果我们把例子中的格式改为”%04d%02d那么sscanf将只返回2day的数值不会被sscanf更改。
浮点数转换
#include stdio.h
#include string.h
#include stdlib.hint main()
{double longitude, latitude;int converted sscanf(113.123456789,31.123456789, %lf,%lf, longitude, latitude);printf(converted%d, longitude%.9lf, latitude%lf\n, converted, longitude, latitude);system(pause);return 0;
}sscanf的格式字符串中f表示这是一个浮点数其修饰词l表示这是一个double的浮点数。
3.3 sscanf高级用法
取到指定字符为止运算符 %[ ]
遇到空格为止
#include stdio.h
#include string.h
#include stdlib.hint main()
{char str[100];sscanf(Lucky xu123, %[^ ], str); //取遇到空格为止字符串 printf(str%s\n, str); system(pause);return 0;
}遇到指定字符为止
#include stdio.h
#include string.h
#include stdlib.hint main()
{char str[100];sscanf(Lucky xu123, %[^1], str); //取遇到空格为止字符串 printf(str%s\n, str); system(pause);return 0;
}我们设定运算符为% [^1] 即遇到1截止最终结果也符合预期。
取仅包含指定字符集
#include stdio.h
#include string.h
#include stdlib.hint main()
{char str[100];sscanf(654321abcdedfABCDEF, %[1-9a-z], str); //只取数字和小写字符printf(str%s\n, str); system(pause);return 0;
}[a-z]表示读取a-z的所有字符[^a-z]表示读取除a-z以外的所有字符 。
取到指定字符集为止
#include stdio.h
#include string.h
#include stdlib.hint main()
{char str[100];sscanf(BCDEF123456abcdedf, %[^a-z], str); //取遇到小写字母为止的字符串 printf(str%s\n, str); system(pause);return 0;
}4 sprintf函数
4.1 背景 在使用STM32驱动TFT屏幕时发现厂家给的驱动函数只支持16位无符号整形数据即可显示的范围为0~65535那么我们想显示65535以外的数则需要自己写驱动函数本着偷懒的原则我发现了厂家提供了字符串驱动函数那么我们只需要将65535以外的数转为字符串进行显示即可。 这便需要使用我们的sprintf函数sprintf的函数原型
#include stdio.h
int sprintf( char *buffer, const char *format, [ argument] … );参数列表 bufferchar型指针指向欲写入的字符串地址。 formatchar型指针指向的内存里面存放了格式字符串。 [argument]…可选参数可以是任何类型的数据。 返回值字符串长度strlen
4.2 sprintf用法
将 %f 格式的数据写入到字符串中
#define _USE_MATH_DEFINES 1 //如果要使用math.h里面的宏需要定义_USE_MATH_DEFINES
#include stdio.h
#include math.h
#include stdlib.hint main()
{char str[80];sprintf(str, Pi 的值 %f, M_PI);puts(str);system(pause);return(0);
}字符串写入字符串中
#include stdio.h
#include stdlib.hint main()
{char dest[20];sprintf(dest, Hello World!);puts(dest);system(pause);return 0;
}多个格式的写入
#include stdio.h
#include stdlib.hint main()
{int num 886;char str[] byebye;char dest[20];sprintf(dest, %s is %d, str, num);puts(dest);system(pause);return 0;
}观察函数的返回值 sprintf函数的返回值不包含目标字符串末尾自动添加的’\0’ #include stdio.h
#include stdlib.hint main()
{int num 886;char str[] byebye;char dest[20];int len sprintf(dest, %s is %d, str, num);puts(dest);printf(len %d\n, len);system(pause);return 0;
}指定起始目标字符串地址 当你想要在一个字符数组的某个位置开始时那么第一个参数就要传对应位置的地址。 #include stdio.h
#include stdlib.hint main()
{char dest[40] I love ;char str[] this world!;sprintf(dest 7, str);puts(dest);system(pause);return 0;
}格式化字符数组
#include stdio.h
#include stdlib.hint main()
{char a[100] { 0 };sprintf(a, 你好,我是%s博主, Jack.xu);printf(%s\n, a);system(pause);return 0;
}字符串的拼接
#include stdio.h
#include stdlib.hint main()
{char dest[100];char str1[] Hello;char str2[] World!;int len1 sprintf(dest, %s, % s, str1, str2);printf(%s\n%d\n, dest, len1);system(pause);return 0;
}数字转换成字符串
#include stdio.h
#include stdlib.hint main()
{unsigned int number 655350;char buffer[10];sprintf(buffer, %d, number);printf(%s\n,buffer);system(pause);return 0;
}在这里我们将655350转化为字符串。可以看出利用sprinrf函数可以解决我们背景中的问题。
5 strcpy函数
5.1 strcpy介绍 strcpy函数是将一个字符串复制到另一块空间地址中的函数‘\0’是停止拷贝的终止条件同时也会将 ‘\0’ 也复制到目标空间。 strcpy的函数原型
#include string.h
char *strcpy(char *dest, const char *src);函数的参数
char *dest------------目标字符串的首地址const char *src------源地址被复制的字符串的首地址用const修饰避免修改掉被拷贝的字符串
函数的返回值类型
char*返回的是目标字符串的首地址
5.1 strcpy用法
#include stdio.h
#include string.h
#include stdlib.hint main()
{char arr[10] { 0 };const char* p abcdef;strcpy(arr, p);printf(%s\n, arr);system(pause);return 0;
}