当前位置:网站首页>C语言常用字符串处理函数
C语言常用字符串处理函数
2022-04-23 03:56:00 【Chenxr32】
gets、strcpy、strncpy函数处理字符串时的特点
字符串输入函数gets:
char *gets(char *s);
gets函数输入一个字符串到字符数组s中。s是字符数组或指向字符数组的指针。
字符串字符串复制函数strcpy、strncpy:
char *strcpy(char *s1,const char*s2);
char *strncpy(char *s1,const char*s2,size_t n);
strcpy函数将s2中字符串复制到s1中,包括空字符。s1是字符数组或指向字符数组的指针,s2是字符常量、字符数组或之向字符数组的指针。strncpy将s2中不超过n个字符的字符复制到s1中,若s2中字符串长度不足n个,则复制整个s2,其余用空字符填充直到n个。
我在使用这两种函数时产生疑问:如果s或s1中原本存有字符串且字符串长度比新输入s或s1的字符串的长度长,那么在输入或复制新字符串后,s或s1中原本存有的字符串是被完全替换还是部分替换。我自己试验了一下,略有发现与大家分享。
一、gets函数
图1
运行图1中程序,我向s中输入了“Hello”并采用了标准输出函数printf和字符串输出函数puts输出新字符串s,得到图2所示结果。
两种输出函数均输出“Hello”,难道用gets函数输入新字符串后,s中原本存有的字符串是被完全替换?我将printf("%s\n",s);改为了printf("%c\n",s[8]);并输入“Hello”,程序对应输出了一个字符w。字符数组s中原来存有的“I love nwpu”似乎没有被完全替换。于是,我改用单个字符输出的方式将字符数组s中的元素逐个输出。
图3
运行图3中程序,输入“Hello”,得到了图4所示结果。
图4
程序输出了“Hello nwpu”,由此可以得出一个结论:用gets函数输入新字符串后,s中原本存有的字符串被部分替换。那么为什么用printf("%s\n",s);或puts(s);输出时程序只输出“Hello”?
值得注意的是,gets函数输入完成后,在输入的字符串末尾自动添加空字符。因此,“Hello”的“o”后有一个空字符,图4中的输出结果实际上是“Hello' \0' nwpu”,“o”与“n”之间是一个空字符和一个空格。puts(s);和printf("%s\n",s);在输出字符串时,只要遇到第一个空字符就结束,而不管是否到了数组的末尾。所以用printf("%s\n",s);或puts(s);输出时程序只输出“Hello”。
由以上实验可以得出更完善的结论:用gets函数输入新字符串后,字符数组s中原本存有的字符串被部分替换。字符数组s中原有字符串被替换的长度为新输入字符串的长度加上一个空字符的长度。
二、strcpy、strncpy函数:
strcpy函数和strncpy函数的实验方式和gets函数类似,一下作简要叙述。
1、strcpy函数:
图5
图6
运行图5中程序,得到图6所示结果。strcpy会将s2中的空字符也复制到s1中,所以s1以字符串形式输出时,printf函数遇到第一个空字符时结束输出;逐个字符输出s1时,“o”与“n”之间是一个空字符和空格。
结论:用strcpy函数将s2中的字符串复制到s1中后,字符数组s1中原本存有的字符串被部分替换。字符数组s1中原有字符串被替换的长度为s2字符串的长度加上一个空字符的长度。
2、strncpy函数:
图7
图8
运行图7中程序,得到图8所示结果。strncpy将s2中不超过n个字符的字符复制到s1中,这里我用的n为2。strncpy只将s2中字符串的前两个字符“h”、“e”复制到s1,看来strncpy不会自动添加空字符,而且是s2中前n个字符部分替换s1中的前n个字符。
据实验结果可知,strcpy和strncpy不会在字符串末尾自动添加空字符。用strcpy复制字符串是可能会由于s1存储空间小而导致数组越界,而strncpy可以避免,但是strncpy复制后可能会使s1没有空字符结束,例如:
char s1[10]; strncpy(s1, " programming language",10);
strncpy用法:
strncpy(s1,s2,sizeof(s1)-1);
s1[sizeof(s1)-1]='\0';
总结:无论是用gets函数向字符数组s中输入字符串还是用strcpy或strncpy函数向字符数组s1中复制字符串,均是新字符串部分替换原有字符串。gets函数会在新字符串末尾自动添加空字符,而strcpy或strncpy只是将s2复制到s1中,不会自动添加空字符。
版权声明
本文为[Chenxr32]所创,转载请带上原文链接,感谢
https://blog.csdn.net/QDchenxr/article/details/79297304
边栏推荐
- ROS series (4): ROS communication mechanism series (4): topic communication practice
- Key point detection of human hand based on mediapipe
- SQL learning record
- A sword is a sword. There is no difference between a wooden sword and a copper sword
- Matlab reads multiple fig graphs and then combines them into one graph (in the form of sub graph)
- Common auxiliary classes
- Mysql出现2013 Lost connection to MySQL server during query
- [AI vision · quick review of today's sound acoustic papers issue 1] Thu, 14 APR 2022
- Wechat applet cloud database value assignment to array error
- Express middleware ② (classification of Middleware)
猜你喜欢
变量、常量、运算符
matlab讀取多張fig圖然後合並為一張圖(子圖的形式)
Second kill all interval related problems
SQL learning record
Basic usage of Google colab (I)
Key point detection of human hand based on mediapipe
标识符、关键字、数据类型
As a code farmer, what kind of experience is it that a girlfriend can code better than herself?
Express中间件①(中间件的使用)
A function second kill 2sum 3sum 4sum problem
随机推荐
How Zotero quotes in word jump to references / hyperlink
vscode删除卸载残余
Concepts of objects and classes
Activity supports multi window display
硬核拆芯片
51 single chip microcomputer: D / a digital to analog conversion experiment
LabVIEW 小端序和大端序区别
Thought of reducing Governance -- detailed summary of binary search
What if win10 doesn't have a local group policy?
2021-09-03 crawler template (only static pages are supported)
Cause analysis of incorrect time of AI traffic statistics of Dahua Equipment Development Bank
[AI vision · quick review of robot papers today, issue 30] Thu, 14 APR 2022
Why is it necessary to divide the variance by 255^2 when adding Gaussian noise using the imnoise function of MATLAB
网络原理 | TCP/IP中的连接管理机制 重要协议与核心机制
Wechat applet canvas draws a simple asymptotic color of the dashboard
Network principle | connection management mechanism in TCP / IP important protocol and core mechanism
Let matlab2018b support the mex configuration of vs2019
Man's life
UDP protocol and TCP protocol
A function second kill 2sum 3sum 4sum problem