当前位置:网站首页>Some notes on the use of C language strings
Some notes on the use of C language strings
2022-06-22 05:07:00 【Jiangxueqian】
String input problem
String input function :
- scanf(“%s”, str) Read in a string of characters , With
Space 、 enter 、 Tab or EOFIs the separator symbol , Space 、 enter 、 Tab or EOF Will not be read in . The character after the separator will be treated as the next input . - gets(str) Read in a line of characters , With
Carriage return or EOFIs the separator symbol , Carriage return or EOF Will not be read in .
Out of commission Assignment statement Will a String constant or A character array Directly assigned to a A character array . The character array name is an address constant , It cannot change the value , Just as a numeric array name cannot be assigned . If the following two lines are illegal :
str1 = "China";
str2 = str1;
use Assignment statement Only one character Give to one Character variables or Character array elements . So a quick implementation is to use strcpy function Put a string Copy Go to another character array .
String copy function :strcpy(str1, str2) hold character string str2 Copy to character array str1 In the middle ( Include ’\0’). character string str2 The location of can also be a String constant . if str2 Valid characters digit <= str1, Will be partially or completely Cover . if str2 Number of valid characters > str1, May be an error .
String refers to the location copy function :strncpy(str1, str2, n) The string str2 in front n individual Copy characters to character array str1 In the middle , replace str1 Original front n individual character .
String length problem
When we initialize the character array , from brackets Whether to write or not Numbers There are two ways to write it :
- Numbers are written in brackets
A. The number is equal to the actual size : SystemIt will not be followed by '\0', The number of characters equals the size of the array equals the number .
B. The number is not equal to the actual size : The system will be rightchar str[6] = "abc123"; char str[6] = { "abc123"}; char str[6] = { 'a', 'b', 'c', '1', '2', '3'};Uninitialized element set '\0', The number of characters is different from that of numbers , The array size is equal to the number .char str[8] = "abc123"; //"abc123\0\0" char str[8] = { "abc123"}; //{"abc123\0\0"} char str[8] = { 'a', 'b', 'c', '1', '2', '3'}; //{'a','b','c','1','2','3','\0','\0'} - Brackets do not write numbers : In the first two cases, the system will
Automatically add... At the end '\0', At this point, the size of the array is equal to the number of charactersAdd one. The third case isDifferent, The length of the array is equal to the number of characters .char str[] = "abc123"; //"abc123\0" char str[] = { "abc123" }; //{"abc123\0"} char str[] = { 'a', 'b', 'c', '1', '2', '3' }; // Therefore, when using this method to define , We usually add... Manually '\0', as follows : //char str[] = { 'a', 'b', 'c', '1', '2', '3', '\0' };
And we generally think of the string length , It refers to the number of valid characters in the character array , So... Is not included ‘\0’.
So using sizeof() Calculated Character array length Can not be used as String length Of , So we usually use strlen() function .strlen() function adopt '\0' To determine the end position of the string , And returns the string Number of valid characters , To calculate the String length .
Other string functions
String concatenation function :strcat(str1, str2)
Put the string str2 Splicing To string str1 Of Back ( Include '\0'), The result is put in str1 Character array , Therefore, we must ensure that str1 The array size meets the requirements . After the function call Return value yes str1 Character array Address .
String comparison function :strcmp(str1, str2)
be used for Compare str1 and str2, The comparison rule is to compare two strings From left to right Press... Character by character ASCII code Value size comparison , Until it appears Different characters Or encounter '\0' until . In fact, when using this function, we are more concerned about whether it equal Result , If All characters identical , Two strings are considered equal . Instead, I don't care who is bigger than who , String size doesn't make sense .
- If two strings are same , return 0.
- if str1>str2, Returns a positive integer .
- if str1<str2, Returns a negative integer .
String case conversion function
- strlwr(): In the string
CapitalizationThe letters are converted toA lowercase letterLetter . - strupr(): In the string
A lowercase letterThe letters are converted toCapitalizationLetter .
边栏推荐
- Gavin's perception of transformer live class - NLU and policies data decryption, source code analysis and practice of insurance BOT in the insurance industry of Rasa dialogue robot project (57)
- Flink deployment mode (I) - standalone and Application
- 【科研笔记】关于使用openslide切图的下采样倍数
- 数据的备份与恢复
- Restframework read and non read sequence processing
- Postman uses (reads) JSON files for batch testing
- Geographic location type of redis
- 花式优化器整理
- Ora-15063: ASM discovered an insufficient number of disks for diskgroup
- Concurrent programming - thread pool
猜你喜欢
![[fault diagnosis] stitch Py script failure](/img/5c/e5df21674b5d0677484b49a608f8ae.png)
[fault diagnosis] stitch Py script failure

Golang concise architecture practice

Target detection algorithm based on deep learning interview essential (rcnn~yolov5)

Final examination questions of Database Principles

Virtual address space

并发编程——线程池
Please, use three JS make 2D pictures have 3D effect cool, OK

Solutions to MySQL 8.0 public key retrieval is not allowed errors

It is easy to analyze and improve R & D efficiency by understanding these five figures

VirtualBox 6.1.34 release
随机推荐
Please, use three JS make 2D pictures have 3D effect cool, OK
@Conditional use
数据的备份与恢复
MySQL notes
Virtual address space
What problems will be encountered during the implementation of MES management system
Start with the strategy of small market value factor, and take you into quantitative investment (with the strategy of 77.83% annualized return)
【chrome】 谷歌小技巧 谷歌浏览器 自带 滚动截图(全屏截图)
Web page design and production final assignment report - College Students' online flower shop
MySQL day01 class notes
Disturbed when programmers are programming? Daily anecdotes
Lottie components make animation easier
How much is London gold
What is the difference between TOC and Tob
In depth understanding of JS delete
图扑软件2D与2.5D案例合集|智慧园区、数据中心、SMT 生产线...
yarn部署模式依赖预上传设置
【sdx62】QCMAP_ CLI manual dialing instructions
数据库---基础知识
The application of RPC technology and its framework sekiro in crawler reverse, encrypting data is a shuttle!