当前位置:网站首页>Common string processing functions in C language
Common string processing functions in C language
2022-04-23 03:59:00 【Chenxr32】
gets、strcpy、strncpy Function when dealing with strings
String input function gets:
char *gets(char *s);
gets Function to enter a string into a character array s in .s Is a character array or a pointer to a character array .
String string copy function strcpy、strncpy:
char *strcpy(char *s1,const char*s2);
char *strncpy(char *s1,const char*s2,size_t n);
strcpy Function will s2 Copy string from to s1 in , Include empty characters .s1 Is a character array or a pointer to a character array ,s2 character constant 、 A pointer to an array of characters .strncpy take s2 Not more than n Copy characters of characters to s1 in , if s2 Insufficient string length in n individual , Copy the entire s2, The rest are filled with empty characters until n individual .
I have questions when using these two functions : If s or s1 The original string exists and the length of the string is longer than that of the new input s or s1 The length of the string is long , Then after entering or copying a new string ,s or s1 Is the original string replaced completely or partially . I tried it myself , A little discovery to share with you .
One 、gets function

chart 1
Operation diagram 1 Medium procedure , I asked s Entered in “Hello” The standard output function printf And string output functions puts Output a new string s, Get the picture 2 The results shown are .

Both output functions output “Hello”, Do you use gets Function after entering a new string ,s The original existing string is completely replaced ? I will printf("%s\n",s); Change it to printf("%c\n",s[8]); And enter the “Hello”, The program outputs a character w. A character array s From the Central Plains “I love nwpu” It doesn't seem to have been completely replaced . therefore , I use the single character output method to convert the character array s The elements in are output one by one .

chart 3
Operation diagram 3 Medium procedure , Input “Hello”, We get the graph 4 The results shown are .

chart 4
The program output “Hello nwpu”, From this we can draw a conclusion : use gets Function after entering a new string ,s The original existing string is partially replaced . So why use printf("%s\n",s); or puts(s); When outputting, the program only outputs “Hello”?
It is worth noting that ,gets After the function input is completed , Automatically add a null character at the end of the input string . therefore ,“Hello” Of “o” There is an empty character after , chart 4 The output in is actually “Hello' \0' nwpu”,“o” And “n” Between them is an empty character and a space .puts(s); and printf("%s\n",s); When outputting a string , It ends as soon as the first null character is encountered , Whether or not it reaches the end of the array . So use printf("%s\n",s); or puts(s); When outputting, the program only outputs “Hello”.
From the above experiments, we can draw a more perfect conclusion : use gets Function after entering a new string , A character array s The original existing string is partially replaced . A character array s The length of the original string to be replaced is the length of the new input string plus the length of an empty character .
Two 、strcpy、strncpy function :
strcpy Functions and strncpy The experimental method of function and gets Function similar to , Make a brief description .
1、strcpy function :

chart 5

chart 6
Operation diagram 5 Medium procedure , Get the picture 6 The results shown are .strcpy Will s2 Empty characters in are also copied to s1 in , therefore s1 When outputting as a string ,printf The function ends the output when it encounters the first null character ; Character by character output s1 when ,“o” And “n” There is an empty character and a space between .
Conclusion : use strcpy Function will s2 Copy the string in to s1 In the after , A character array s1 The original existing string is partially replaced . A character array s1 The length of the original string to be replaced is s2 The length of the string plus the length of an empty character .
2、strncpy function :

chart 7

chart 8
Operation diagram 7 Medium procedure , Get the picture 8 The results shown are .strncpy take s2 Not more than n Copy characters of characters to s1 in , I used it here n by 2.strncpy Only will s2 The first two characters of the string in “h”、“e” Copied to the s1, It seems strncpy Empty characters are not automatically added , And it's s2 Middle front n Partial replacement of characters s1 In front of n Characters .
According to the experimental results ,strcpy and strncpy Empty characters are not automatically added at the end of the string . use strcpy Copying strings is possible due to s1 The storage space is small and the array is out of bounds , and strncpy You can avoid , however strncpy Copying may make s1 There is no empty character , for example :
char s1[10]; strncpy(s1, " programming language",10);
strncpy usage :
strncpy(s1,s2,sizeof(s1)-1);
s1[sizeof(s1)-1]='\0';
summary : Whether it's for use gets Array of characters s Enter a string or use strcpy or strncpy Array of characters s1 Copy string from , All new strings replace the original strings .gets The function automatically adds an empty character at the end of the new string , and strcpy or strncpy Just to s2 Copied to the s1 in , Empty characters are not automatically added .
版权声明
本文为[Chenxr32]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230356325502.html
边栏推荐
- Now is the best time to empower industrial visual inspection with AI
- [mathematical modeling] my mathematical memory
- Common auxiliary classes
- Nel ASA:挪威Herøya设施正式启用
- TreeSet课后练习
- How to introduce opencv into cmake project
- Digital image processing third edition Gonzalez notes Chapter 2
- [BIM introduction practice] Revit building wall: detailed picture and text explanation of structure, envelope and lamination
- vscode删除卸载残余
- Summary of knowledge map (I)
猜你喜欢

Machine translation baseline

Win10 boot VMware virtual machine boot seconds blue screen problem perfect solution

硬核拆芯片

Hard core chip removal

Now is the best time to empower industrial visual inspection with AI

Concepts of objects and classes

知乎有问题,谁来解答?

Xiaomi, qui a établi le plus grand volume de ventes de téléphones portables domestiques sur le marché d'outre - mer, se concentre de nouveau sur le marché intérieur

Does China Mobile earn 285 million a day? In fact, 5g is difficult to bring more profits, so where is the money?
![[AI vision · quick review of NLP natural language processing papers today, issue 30] Thu, 14 APR 2022](/img/cc/21b20f56a7eb3e43b9c23792c597ef.png)
[AI vision · quick review of NLP natural language processing papers today, issue 30] Thu, 14 APR 2022
随机推荐
Picture synthesis video
Qtspim manual - Chinese Translation
C language character constant
Installation and configuration of MinGW under win10
matlab讀取多張fig圖然後合並為一張圖(子圖的形式)
减治思想——二分查找详细总结
創下國產手機在海外市場銷量最高紀錄的小米,重新關注國內市場
What if win10 doesn't have a local group policy?
Vs Studio modifie le langage C scanf et d'autres erreurs
现货黄金操作技巧_估波曲线
VHDL语言实现32位二进制数转BCD码
将编译安装的mysql加入PATH环境变量
Definition, understanding and calculation of significant figures in numerical analysis
创下国产手机在海外市场销量最高纪录的小米,重新关注国内市场
【测绘程序设计】坐标反算神器V1.0(附C/C#/VB源程序)
Thought of reducing Governance -- detailed summary of binary search
[BIM introduction practice] wall hierarchy and FAQ in Revit
2021-09-03 crawler template (only static pages are supported)
STM32上μC/Shell移植与应用
Win10 boot VMware virtual machine boot seconds blue screen problem perfect solution