当前位置:网站首页>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
边栏推荐
- 【李宏毅2022 机器学习春】hw6_GAN(不懂..)
- Xiaohongshu was exposed to layoffs of 20% as a whole, and the internal volume among large factories was also very serious
- The whole process of connecting the newly created unbutu system virtual machine with xshell and xftp
- Install PaddlePaddle on ARM
- ROS series (I): rapid installation of ROS
- Definition, understanding and calculation of significant figures in numerical analysis
- [AI vision · quick review of NLP natural language processing papers today, issue 30] Thu, 14 APR 2022
- Opencv4 QR code recognition test
- Identificateur, mot - clé, type de données
- How to introduce opencv into cmake project
猜你喜欢
Solve the technical problems in seq2seq + attention machine translation
[BIM introduction practice] Revit building wall: detailed picture and text explanation of structure, envelope and lamination
[AI vision · quick review of robot papers today, issue 30] Thu, 14 APR 2022
Win10 boot VMware virtual machine boot seconds blue screen problem perfect solution
What if you encounter symbols you don't know in mathematical formulas
Matlab reads multiple fig graphs and then combines them into one graph (in the form of sub graph)
Key point detection of human hand based on mediapipe
SQL learning record
The great gods in acmer like mathematics very much
中国移动日赚2.85亿很高?其实是5G难带来更多利润,那么钱去哪里了?
随机推荐
OpenCV----YOLACT实例分割模型推理
Let matlab2018b support the mex configuration of vs2019
小红书被曝整体裁员20%,大厂之间内卷也很严重
Numpy's broadcasting mechanism (with examples)
【测绘程序设计】坐标反算神器V1.0(附C/C#/VB源程序)
STM32上μC/Shell移植与应用
According to the category information and coordinate information of the XML file, the category area corresponding to the image is pulled out and stored in the folder.
Matlab reads multiple fig graphs and then combines them into one graph (in the form of sub graph)
ROS series (IV): ROS communication mechanism series (2): Service Communication
Photoshop installation under win10
Opencv4 QR code recognition test
Express中间件①(中间件的使用)
[BIM introduction practice] Revit building wall: detailed picture and text explanation of structure, envelope and lamination
ROS series (IV): ROS communication mechanism series (3): parameter server
Now is the best time to empower industrial visual inspection with AI
matlab讀取多張fig圖然後合並為一張圖(子圖的形式)
Vs studio modifies C language scanf and other errors
Thought of reducing Governance -- detailed summary of binary search
Process seven state transition diagram
VHDL语言实现32位二进制数转BCD码