C语言中常用的字符串函数(strlen、sizeof、sscanf、sprintf、strcpy)

这篇具有很好参考价值的文章主要介绍了C语言中常用的字符串函数(strlen、sizeof、sscanf、sprintf、strcpy)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

C语言中常用的字符串函数

1 strlen函数

  strlen即为string length,顾名思义该函数是用来求字符串长度的。在介绍strlen函数之前,我们要先介绍一下"\0"这个转义字符。任何一个字符串后面都会隐藏一个"\0",该转义字符是字符串结束的标志,所以我们在使用strlen函数求字符串长度时,遇到"\0"时停止读取,此时"\0"前的字符个数就是字符串的长度。

注意:
  这里的"\0"只是结束标志,并不算一个字符!

示例1:

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

int main()
{
    printf("%d\n", strlen("xxccyy"));
    
    system("pause");

    return 0;
}

C语言中常用的字符串函数(strlen、sizeof、sscanf、sprintf、strcpy),c语言
我们将字符串"xxccyy"在内存的存储结构展示如下:
C语言中常用的字符串函数(strlen、sizeof、sscanf、sprintf、strcpy),c语言

示例2:
  在c语言中,字符串并没有作为一种独立的数据类型进行定义。相反,字符串被表示为字符数组或字符指针。以下是两种常见的表示字符串的方法:

  1. 使用字符数组:
char str[20] = "Hello, World!"; // 声明一个字符数组来存储字符串
  1. 使用字符指针:
char *str = "Hello, World!"; // 声明一个指向字符的指针,指向字符串常量

  接下来我们介绍使用字符数组存储字符串的每一个字符,使用这种定义方式对于strlen的求解有何不同呢?

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

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

C语言中常用的字符串函数(strlen、sizeof、sscanf、sprintf、strcpy),c语言
  arr1数组只是单纯把字符串“abcdef”的每一个字符用数组存储起来,而arr2数组则是多存储了一个“\0",可以看到arr1数组的长度为9,arr2数组的长度为6,接下来我们将展示两个数组在内存中的存储状态。
C语言中常用的字符串函数(strlen、sizeof、sscanf、sprintf、strcpy),c语言
C语言中常用的字符串函数(strlen、sizeof、sscanf、sprintf、strcpy),c语言
   arr2数组的存储情况和示例1字符串的存储情况相同,而arr1却不同。对于arr2我们不进行说明,接下来我们分析下为什么arr1数组的长度为9。
  上文我们说过字符串结束标志为"\0",但是我们的arr1数组没有额外存储"\0",所以编译器在读取时,并不会像我们所期望的那样停止读取,故长度当然不会为6。但是为什么最终读取的长度为9,是因为在读取时,编译器读取完arr1时会继续往后读取,直到读取到”\0",arr1在读取完第9个字符后才会遇到”\0";由于每个人的电脑和编译器不同,读取的长度也不一样,所以arr1这种情况一般我们认为它读取的结果为随机值!

示例3:

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

int main()
{

    printf("%d\n", strlen("xcy\0zfr"));
    
    system("pause");

    return 0;
}

C语言中常用的字符串函数(strlen、sizeof、sscanf、sprintf、strcpy),c语言

示例3是为了进一步说明字符串结束标志"\0"的重要性。

2 sizeof函数

2.1 sizeof介绍

  sizeof是计算变量在内存的占空间的大小,单位是字节。

2.2 sizeof用法

  • 使用sizeof查看数据类型占空间大小
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

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

C语言中常用的字符串函数(strlen、sizeof、sscanf、sprintf、strcpy),c语言

  • 使用sizeof计算基本数据类型变量的占用空间的大小
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

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

C语言中常用的字符串函数(strlen、sizeof、sscanf、sprintf、strcpy),c语言

  • 使用sizeof计算指针的占用空间大小
      需要注意的是,32位平台所有类型的指针的占用空间大小都是4个字节,64位平台所有类型的指针占用的空间大小为8个字节,观察如下代码:
#include <stdio.h>
#include <stdlib.h>


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

C语言中常用的字符串函数(strlen、sizeof、sscanf、sprintf、strcpy),c语言

  • 计算数组元素的个数

如想得到数组的元素个数,有以下两种方法:
1.总长度/相对应的数据类型长度
2.总长度/首元素长度

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

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

C语言中常用的字符串函数(strlen、sizeof、sscanf、sprintf、strcpy),c语言

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.h>

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

C语言中常用的字符串函数(strlen、sizeof、sscanf、sprintf、strcpy),c语言
  “%04d%02d%02d"是用来解析字符串的格式,%表示格式转换的开始,d表示转换为一个整数,04作为d的修饰,表示这是一个长度为4位的整数,不足4位时以0补齐。
  返回值converted等于3,表示有3个数据成功转换,转换成功数目同时取决于被解析的字符串以及其转换格式,如果我们把例子中的格式改为”%04d%02d",那么sscanf将只返回2,day的数值不会被sscanf更改。

  • 浮点数转换
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

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

C语言中常用的字符串函数(strlen、sizeof、sscanf、sprintf、strcpy),c语言
sscanf的格式字符串中,f表示这是一个浮点数,其修饰词l表示这是一个double的浮点数。

3.3 sscanf高级用法

取到指定字符为止:运算符 %[ ]

  • 遇到空格为止
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{
    char str[100];
    sscanf("Lucky xu123", "%[^ ]", str); //取遇到空格为止字符串    
    printf("str=%s\n", str); 

    system("pause");

    return 0;
}

C语言中常用的字符串函数(strlen、sizeof、sscanf、sprintf、strcpy),c语言

  • 遇到指定字符为止
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{
    char str[100];
    sscanf("Lucky xu123", "%[^1]", str); //取遇到空格为止字符串    
    printf("str=%s\n", str); 

    system("pause");

    return 0;
}

C语言中常用的字符串函数(strlen、sizeof、sscanf、sprintf、strcpy),c语言
  我们设定运算符为% [^1] ,即遇到1截止,最终结果也符合预期。

  • 取仅包含指定字符集
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{
    char str[100];
    sscanf("654321abcdedfABCDEF", "%[1-9a-z]", str); //只取数字和小写字符
    printf("str=%s\n", str);  

    system("pause");

    return 0;
}

C语言中常用的字符串函数(strlen、sizeof、sscanf、sprintf、strcpy),c语言
[a-z]表示读取a-z的所有字符,[^a-z]表示读取除a-z以外的所有字符 。

  • 取到指定字符集为止
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{
    char str[100];
    sscanf("BCDEF123456abcdedf", "%[^a-z]", str); //取遇到小写字母为止的字符串       
    printf("str=%s\n", str);   

    system("pause");

    return 0;
}

C语言中常用的字符串函数(strlen、sizeof、sscanf、sprintf、strcpy),c语言

4 sprintf函数

4.1 背景

  在使用STM32驱动TFT屏幕时,发现厂家给的驱动函数只支持16位无符号整形数据,即可显示的范围为0~65535,那么我们想显示65535以外的数则需要自己写驱动函数,本着偷懒的原则我发现了厂家提供了字符串驱动函数,那么我们只需要将65535以外的数转为字符串进行显示即可。
C语言中常用的字符串函数(strlen、sizeof、sscanf、sprintf、strcpy),c语言
这便需要使用我们的sprintf函数,sprintf的函数原型:

#include <stdio.h>
int sprintf( char *buffer, const char *format, [ argument]);

参数列表:
buffer:char型指针,指向欲写入的字符串地址。
format:char型指针,指向的内存里面存放了格式字符串。
[argument]…:可选参数,可以是任何类型的数据。
返回值:字符串长度(strlen)

4.2 sprintf用法

  • 将 %f 格式的数据写入到字符串中
#define _USE_MATH_DEFINES 1  //如果要使用<math.h>里面的宏,需要定义_USE_MATH_DEFINES 
#include <stdio.h>
#include <math.h>
#include <stdlib.h>

int main()
{
	char str[80];
	
	sprintf(str, "Pi 的值 = %f", M_PI);
	puts(str);

    system("pause");
	return(0);
}

C语言中常用的字符串函数(strlen、sizeof、sscanf、sprintf、strcpy),c语言

  • 字符串写入字符串中
#include <stdio.h>
#include <stdlib.h>

int main()
{
	char dest[20];
	sprintf(dest, "Hello World!");
	puts(dest);

    system("pause");
    return 0;
}

C语言中常用的字符串函数(strlen、sizeof、sscanf、sprintf、strcpy),c语言

  • 多个格式的写入
#include <stdio.h>
#include <stdlib.h>

int main()
{
	int num = 886;
	char str[] = "byebye";
	char dest[20];
	sprintf(dest, "%s is %d", str, num);
    puts(dest);

    system("pause");
    return 0;
}

C语言中常用的字符串函数(strlen、sizeof、sscanf、sprintf、strcpy),c语言

  • 观察函数的返回值

sprintf函数的返回值不包含目标字符串末尾自动添加的’\0’

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

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

C语言中常用的字符串函数(strlen、sizeof、sscanf、sprintf、strcpy),c语言

  • 指定起始目标字符串地址

当你想要在一个字符数组的某个位置开始时,那么第一个参数就要传对应位置的地址。

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

int main()
{
	char dest[40] = "I love ";
	char str[] = "this world!";
    sprintf(dest + 7, str);
	puts(dest);

    system("pause");
    return 0;
}

C语言中常用的字符串函数(strlen、sizeof、sscanf、sprintf、strcpy),c语言

  • 格式化字符数组
#include <stdio.h>
#include <stdlib.h>

int main()
{
	char a[100] = { 0 };
	sprintf(a, "你好,我是%s博主", "Jack.xu");
	printf("%s\n", a);

    system("pause");
    return 0;
}

C语言中常用的字符串函数(strlen、sizeof、sscanf、sprintf、strcpy),c语言

  • 字符串的拼接
#include <stdio.h>
#include <stdlib.h>

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

C语言中常用的字符串函数(strlen、sizeof、sscanf、sprintf、strcpy),c语言

  • 数字转换成字符串
#include <stdio.h>
#include <stdlib.h>

int main()
{
	unsigned int number = 655350;
	char buffer[10];
	sprintf(buffer, "%d", number);
	printf("%s\n",buffer);

    system("pause");
    return 0;
}

C语言中常用的字符串函数(strlen、sizeof、sscanf、sprintf、strcpy),c语言
  在这里我们将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.h>

int main()
{
	char arr[10] = { 0 };
	const char* p = "abcdef";
	strcpy(arr, p);
	printf("%s\n", arr);

    system("pause");
	return 0;
}

C语言中常用的字符串函数(strlen、sizeof、sscanf、sprintf、strcpy),c语言文章来源地址https://www.toymoban.com/news/detail-800201.html

到了这里,关于C语言中常用的字符串函数(strlen、sizeof、sscanf、sprintf、strcpy)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • PHP strlen()函数详解,PHP获取字符串长度

    「作者主页」: 士别三日wyx 「作者简介」: CSDN top100、阿里云博客专家、华为云享专家、网络安全领域优质创作者 「推荐专栏」: 对网络安全感兴趣的小伙伴可以关注专栏《网络安全入门到精通》 strlen() 可以返回 「字符串」 的 「长度」 。 语法 参数 $str :需要计算长度

    2024年02月15日
    浏览(31)
  • C语言字符串转换double等类型(sscanf,atof,strod)

    例子: 注意: 忽略空格   例子: 注意: sscanf解析字符串时,空格通常被用作分隔符,可以用它来分隔字符串中的不同部分。 例如,以下代码将把字符串\\\"123 456\\\"中的两个整数读入a和b中,空格用作分隔符: 在这个例子中,sscanf会忽略字符串中的多余空格,并将\\\"123\\\"解析为a的

    2024年02月14日
    浏览(36)
  • 【c语言:常用字符串函数与内存函数的使用与实现】

    简介:本篇文章是对C语言中常用的字符串函数和内存函数的学习以及模拟实现 文档内容来自:https://legacy.cplusplus.com/ 字符串以 ‘\\0’ 作为结束标志, strlen函数返回的是在字符串中 ‘\\0’ 前⾯出现的字符个数( 不包含 \\\'\\0\\\' )。 参数指向的字符串必须要以 ‘\\0’ 结束。 注意函

    2024年02月04日
    浏览(32)
  • c语言进阶部分详解(详细解析字符串常用函数,并进行模拟实现(下))

    上篇文章介绍了一些常用的字符串函数,大家可以跳转过去浏览一下:c语言进阶部分详解(详细解析字符串常用函数,并进行模拟实现(上))_总之就是非常唔姆的博客-CSDN博客 今天接着来介绍一些:  目录 一.字符串查找 1.strstr() 1.1示例 1.2注意事项: 1.3模拟实现  2.

    2024年02月07日
    浏览(33)
  • 【C语言】指针进阶之sizeof和strlen函数的对比

    目录  1.sizeofyu 2.strlen函数   3.sizeof与strlen的对比   sizeof计算变量所占内存内存空间 大小 的,单位是 字节 ,如果操作数是类型的话,计算的是使⽤类型创建的变量所占内存空间的大小。 sizeof 只关注占⽤内存空间的大小,不在乎内存中存放什么数据。 举个例子: 运行结果

    2024年01月22日
    浏览(44)
  • C语言:字符函数和字符串函数(一篇拿捏字符串函数!)

    目录 求字符串长度: 1. strlen(字符串长度) 长度不受限制函数: 2. strcpy(字符串拷贝) 3. strcat(字符串追加) 4. strcmp(字符串比较) 长度受限制函数: 5. strncpy(字符串拷贝) 6. strncat(字符串追加) 7. strncmp(字符串比较) 字符串查找: 8. strstr(查找字符串子串) 9. strtok(字符串分割) 错误信

    2024年02月10日
    浏览(83)
  • PHP 字符串常用函数

    strlen 获取指定字符串长度 语法: strpos 用于查找指定字符串,在源字符串 首次 出现的位置(下标),如果没找到则返回 false 注: 大小写不敏感 语法: strrpos 用于查找指定字符串,在源字符串 最后 出现的位置(下标),如果没找到则返回 false 注: 大小写不敏感 语法: st

    2024年02月06日
    浏览(34)
  • Python字符串的常用函数

    Python中用于操作字符串的函数有很多,以下是一些常用的函数及其用法: 1. len():返回字符串的长度    ```    s = \\\"hello, world!\\\"    print(len(s)) # 13    ``` 2. str():将对象转换为字符串类型    ```    n = 123    s = str(n)    print(s) # \\\"123\\\"    ``` 3. upper():将字符串中所有字母都转换

    2024年02月10日
    浏览(36)
  • 【C语言】字符函数,字符串函数,内存函数

    大家好!今天我们来学习C语言中的字符函数,字符串函数和内存函数。 目录 1. 字符函数 1.1 字符分类函数 1.2 字符转换函数 1.2.1 tolower(将大写字母转化为小写字母) 1.2.2 toupper(将小写字母转化为大写字母) 2. 字符串函数 2.1 字符串输入函数 2.1.1 gets() ​2.1.2 fgets() 2.2 字符串

    2024年02月10日
    浏览(46)
  • 【C语言】字符函数和字符串函数

      目录 1.求字符串长度strlen 2.长度不受限制的字符串函数 字符串拷贝strcpy 字符串追加strcat 字符串比较strcmp 3.长度受限制的字符串函数介绍strncpy strncat ​编辑strncmp 4.字符串查找strstr 5.字符串分割strtok 6.错误信息报告 strerror perror 7.字符分类函数 8.字符转换函数  9.内存操作函

    2024年02月12日
    浏览(43)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包