当前位置:网站首页>[C language - zero foundation lesson 13] the mystery of string
[C language - zero foundation lesson 13] the mystery of string
2022-07-27 09:15:00 【Super Daxiong】
Preface
Blogger :Super Daxiong ( A cute new blogger )
C Language column :0 Basic science C Language column
LeetCode special column :LeetCode special column
This issue is about the mystery of string , If there is any mistake, please point out thank you !
Recommend one to my friends Study 、 Brush problem Website ?
Various Interview questions have everything that one expects to find , Brush questions until you feel soft !
All kinds of Internet Learning materials , The real interview questions of major factories start from Start learning from scratch , Help you cope easily Various interview questions , Come and enrich yourself !
Catalog
String input and output functions
What is a string ?
In our impression, it seems to be Chinese characters and symbols . And in the C In language, values consist of numbers 、 Letter 、 A string of characters made up of underscores . Of course, I think there are our Chinese characters , Of course, Chinese characters also belong to Chinese strings . What do strings do ? Integer and real are used to store integer and real variables , We can guess that string is used to store string variables . That's right , It's true . stay C The use of a single character in a language char Type to store , A type larger than one character cannot be used to store . So we use the array , Because arrays can store multiple data of the same type .
In the string ’\0‘
'\0' Is the identification that determines the end of the character array , Indicates that this string of characters has reached the end ; or '\0' It's the end of a string , Any string is automatically followed by '\0'. And the characters don't ‘\0’, Only the string has, at the end of the string, we can't see . The computer will automatically add... After the string ’\0‘.
Definition of string
The array used to store character data is character array , An element in a string array holds a character .
char a[4];Initialization of string
char a[5]={'A','B','C','D','E'};
char b[5]={'A','B'};
char c[]={'A','B'};Be careful : There is no after the character array \0 Of
Character arrays and strings
char a[5]={"ABCD"};
char b[5]="ABCD";
char c[5]="ABCD!"
char d[]="ABCD!"result :
A character array a The length of is 5 Only saved 4 The last element is a \0 One byte of
A character array b The length of is 5 Only saved 4 The last element is a \0 One byte of
A character array c The length of is 5 Save 5 The last element is not \0 Of
A character array d There is no defined length of, so there is also a... At the end \0 So d The length of is 6
ask char[] e=“helloworld”; This array e How many elements are there ?
Input and output of string
Strings can take advantage of scanf function and printf function
char a[5]="ABCD";
printf("%c\n",a[1]);
printf("%s",a);result :
B
ABCD
%c Is to output a single character %s It's the output string
String input and output functions
String input function gets
String output function puts
char a[20];
gets(a);
puts(a); char a[20]="abc";
char b[20]="cba";
puts(a);
puts(b);result :
abc
cba
Be careful :puts and gets Only used for input and output of string .
There are always spaces in the string. You must use gets(),puts()
puts() Automatic line feed effect
gets() Auto wrap when carriage return symbol is encountered
String function
Strlen function
Strlen The function is a function of the length of the test string , barring ’\0’
printf("%d\n",strlen("abc"));
printf("%d\n", strlen("ab\0c"));
printf("%d\n", strlen("\t\t\077"));result :
3
2
3
String copy function strcpy
form :strcpy( character string 1, character string 2)
Use : The string 2 Copy to string 1 in
Be careful : character string 1 The length of cannot be less than the string 2 The length of
char a[20] = "";
char b[20] = "def";
strcpy(a, b);
puts(a);
puts(b);result :
def
def
String concatenation function strcat
form :strcat( A character array 1, A character array 2)
Use : Connect strings in two character arrays , Put the string 2 Received string 1 after
Be careful : character string 1 The array must be large enough , To accommodate the characters after the link , String after connection 1 The back ‘\0‘ Cancel , Keep only the last ’\0‘
char a[20] = "abc";
char b[20] = "def";
strcat(a, b);
puts(a);
puts(b);result :
abcdef
def
String comparison function strcmp
form :strcmp( character string 1, character string 2)
Use : Compare strings 1 And string
Be careful : When comparing two strings, compare them one by one from left to right according to Ascil Compare the size of the code
form | result |
character string 1> character string 2 | 1 |
character string 1= character string 2 | 0 |
character string 1< character string 2 | -1 |
If there is only one, the comparison is completed once , If there are multiple, if the front is equal, compare the next one until it is the last one .
printf("%d\n",strcmp("abc","abc"));
printf("%d\n", strcmp("abd", "abc"));
printf("%d\n", strcmp("aba", "abc"));result :
0
1
-1
Recommend one to my friends Study 、 Brush problem Website ?
Various Interview questions have everything that one expects to find , Brush questions until you feel soft !
All kinds of Internet Learning materials , The real interview questions of major factories start from Start learning from scratch , Help you cope easily Various interview questions , Come and enrich yourself !
边栏推荐
- [cloud native kubernetes practice] deploy the rainbow platform under the kubernetes cluster
- Rewrite the tensorrt version deployment code of yolox
- The execution sequence of async/await, macro tasks and micro tasks
- Mangodb simple to use
- Five kinds of 3D attention/transformer finishing (a-scn, point attention, CAA, offset attention, point transformer)
- 基于ArkUI eTS开发的坚果笑话(NutJoke)
- Is the operation of assigning values to int variables atomic?
- Mangodb简单使用
- Pyqt5 rapid development and practice 4.1 qmainwindow
- Explanation of common basic controls for C # form application (suitable for Mengxin)
猜你喜欢

ES6 new symbol data type

500 error reporting

Explanation of common basic controls for C # form application (suitable for Mengxin)

pollFirst(),pollLast(),peekFirst(),peekLast()

Mangodb simple to use

ES6 new - object part

Aruba learning notes 10 security authentication portal authentication (web page configuration)

Deep understanding of Kalman filter (3): multidimensional Kalman filter

Pyqt5 rapid development and practice 4.1 qmainwindow

js call和apply
随机推荐
New year's goals! The code is more standardized!
[daily algorithm day 96] Tencent interview question: merge two ordered arrays
Flex layout (actual Xiaomi official website)
ES6 new - deconstruction assignment of array / object
STL container - basic operation of queue and deque
Interface test tool - JMeter pressure test use
STL container -- Application of set set
【云驻共创】华为云:全栈技术创新,深耕数字化,引领云原生
C# 窗体应用常用基础控件讲解(适合萌新)
Nutrecipes developed based on arkui ETS
[cloud native kubernetes practice] deploy the rainbow platform under the kubernetes cluster
async/await的执行顺序以及宏任务和微任务
8 kinds of visual transformer finishing (Part 1)
Wechat applet 5 - foundation strengthening (not finished)
Longest string without duplicate characters
500 error reporting
巴比特 | 元宇宙每日必读:广州南沙发布“元宇宙九条”措施,平台最高可获得2亿元资金支持...
Install Oracle under Linux, connect local pl/sql to Oracle import table under Linux, and create new user and password
Primary function t1744963 character writing
Explanation of binary tree