当前位置:网站首页>character string
character string
2022-06-25 23:29:00 【Soy sauce;】
1. String concept and basic operation
String is based on 0 perhaps '\0' Array of characters at the end ,( Numbers 0 And character '\0' Equivalent ), Character arrays can only be initialized 5 Characters , When output , From the beginning until you find 0 end
Character array part initialization , Residual filling 0
If initialized as a string , Then the compiler will add... At the end of the string by default '\0'
explain :
sizeof Calculate array size , The array contains '\0' character
strlen Calculate the length of the string , To '\0' end
Sample code :
// String basic operation // String is based on 0 perhaps '\0' Array of characters at the end ,( Numbers 0 And character '\0' Equivalent ) void test01(){ // Character arrays can only be initialized 5 Characters , When output , From the beginning until you find 0 end char str1[] = { 'h', 'e', 'l', 'l', 'o' }; Garbled ! Because we couldn't find it \0 printf("%s\n",str1); // Character array part initialization , Residual filling 0 char str2[100] = { 'h', 'e', 'l', 'l', 'o' }; There will be no garbled code , Because the rest of the positions are filled automatically 0 printf("%s\n", str2); // If initialized as a string , Then the compiler will add... At the end of the string by default '\0' char str3[] = "hello"; Bring their own \0 printf("%s\n",str3); printf("sizeof str:%d\n",sizeof(str3));6 printf("strlen str:%d\n",strlen(str3));5 //sizeof Calculate array size , The array contains '\0' character //strlen Calculate the length of the string , To '\0' end // So if I write , What's the result ? char str4[100] = "hello"; printf("sizeof str:%d\n", sizeof(str4));100 printf("strlen str:%d\n", strlen(str4));5 // What is the input result below ?sizeof What's the result ?strlen What's the result ? char str5[] = "hello\0world"; printf("%s\n",str5);hello printf("sizeof str5:%d\n",sizeof(str5));12 printf("strlen str5:%d\n",strlen(str5));5 // Again, what is the input result below ?sizeof What's the result ?strlen What's the result ? char str6[] = "hello\012world";\012 Count one printf("%s\n", str6);hello Line break world printf("sizeof str6:%d\n", sizeof(str6));12 printf("strlen str6:%d\n", strlen(str6));11 } |
Octal and hexadecimal escape characters :
stay C There are two special characters in , Octal escape character and hexadecimal escape character , The general form of octal characters is '\ddd',d yes 0-7 The number of . The general form of hexadecimal characters is '\xhh',h yes 0-9 or A-F One in . Octal characters and hexadecimal characters represent characters ASCII The value corresponding to the code . To 10 Binary pair ascll Table character such as :
|

2. The string copy function implements the operation
The premises must be initialized first , Don't be a wild pointer // Copy method 1--- Copy with array subscript ( In fact, it is also a pointer ) The array subscript in the function is equivalent to *(p+i) void copy_string01(char* dest, char* source ){// Take out the memory one by one , Put in another one , Until the Terminator for (int i = 0; source[i] != '\0';i++){ dest[i] = source[i]; } } // Copy method 2--- Copy with pointer void copy_string02(char* dest, char* source){ while (*source != '\0' /* *source != 0 */){ Traverse a whole string , The two first addresses are incremented in steps , Then assign a value *dest = *source; source++; dest++; } } // Copy method 3( More difficult ) void copy_string03(char* dest, char* source){ // Judge *dest Is it 0,0 Then exit the loop while (*dest++ = *source++){} } |

3. String inversion model operation

Method 1: Using subscripts void reverse_string(char* str){ if (str == NULL){ return; } int begin = 0; int end = strlen(str) - 1; while (begin < end){ Judge when the head coordinate is greater than the tail coordinate , Can't be equal , Because even numbers are never equal // Swap two character elements char temp = str[begin]; str[begin] = str[end]; str[end] = temp; begin++; end--; } } void test(){ char str[] = "abcdefghijklmn"; printf("str:%s\n", str); reverse_string(str); printf("str:%s\n", str); } |
Method 2: Using string pointers

4. Formatting of strings
sprintf
#include <stdio.h> int sprintf(char *str, const char *format, ...); function : According to the parameters format String to convert and format data , Then output the result to str In the specified space , until The string Terminator... Appears '\0' until . Parameters : str: String first address , After formatting, fill the address with characters. The data type should be developed char shape format: String format , Usage and printf() equally ...: Parameters in formatting ( Multi bit ) Return value : success : The number of characters actually formatted (strlen Result ) Failure : - 1 |
Format type :
void test(){ //1. Formatted string ( Commonly used ) It is better not to be a null pointer or a wild pointer char buf[1024] = { 0 }; sprintf(buf, " Hello ,%s, Welcome to join us !", "John"); printf("buf:%s\n",buf); memset(buf, 0, 1024); sprintf(buf, " I this year %d Year old !", 20); printf("buf:%s\n", buf); //2. String concatenation memset(buf, 0, 1024); char str1[] = "hello"; char str2[] = "world"; int len = sprintf(buf,"%s %s",str1,str2); printf("buf:%s len:%d\n", buf,len); //3. Number to string formatted output memset(buf, 0, 1024); int num = 100; sprintf(buf, "%d", num); printf("buf:%s\n", buf); // Set width Right alignment memset(buf, 0, 1024); sprintf(buf, "%8d", num); The total width is 8, Right alignment printf("buf:%s\n", buf); // Set width Align left memset(buf, 0, 1024); sprintf(buf, "%-8d", num); printf("buf:%s\n", buf); // Turn into 16 Base string A lowercase letter memset(buf, 0, 1024); sprintf(buf, "0x%x", num); printf("buf:%s\n", buf); // Turn into 8 Base string memset(buf, 0, 1024); sprintf(buf, "0%o", num); printf("buf:%s\n", buf); } |
边栏推荐
- 【2023校招刷题】番外篇1:度量科技FPGA岗(大致解析版)
- 1281_ FreeRTOS_ Implementation analysis of vtaskdelayuntil
- UE4_UE5结合offline voice recognition插件做语音识别功能
- [opencv450 samples] create image list yaml
- ES6 learning -- let
- Equivalence class, boundary value, application method and application scenario of scenario method
- The wisdom of questioning? How to ask questions?
- Ue4 Ue5 combine le plug - in de reconnaissance vocale de bureau pour la reconnaissance vocale
- golang Make a list of intervals with sequential numbers
- 【opencv450-samples】读取图像路径列表并保持比例显示
猜你喜欢

LM小型可编程控制器软件(基于CoDeSys)笔记十七:pto脉冲功能块
![[modulebuilder] GP service realizes the intersection selection of two layers in SDE](/img/4a/899a3c2a0505d2ec2eaae97a3948c9.png)
[modulebuilder] GP service realizes the intersection selection of two layers in SDE

【无标题】打开一个项目连接,无法正常显示时,ping一下ip

Unity的Ping类使用

UE4\UE5 蓝图节点Delay与Retriggerable Delay的使用与区别

UE4 学习记录二 给角色添加骨架,皮肤,及运动动画

Use and difference between ue4\ue5 blueprint node delay and retroggable delay

Circuit module analysis exercise 5 (power supply)

Pycharm student's qualification expires, prompting no suitable licenses associated with account solution
![[untitled] open an item connection. If it cannot be displayed normally, Ping the IP address](/img/34/d3c146d5faa2728cb5eb8f3ee00200.png)
[untitled] open an item connection. If it cannot be displayed normally, Ping the IP address
随机推荐
Some points to pay attention to when closing mongodb services (as well as related commands when opening)
Technology blog site collection
ES6 - numerical extension and object extension
汇编语言核心要点
To solve the incompatibility between VM and device/credential guard, an effective solution for the whole network
STM32 development board + smart cloud aiot+ home monitoring and control system
[2023 proofreading and bidding questions] Part 1: Measurement Technology FPGA post (roughly analytical version)
1281_ FreeRTOS_ Implementation analysis of vtaskdelayuntil
Determine whether the appointment time has expired
Leetcode(435)——无重叠区间
判断预约时间是否已经过期
#24class静态成员
最近准备翻译外国优质文章
等价类,边界值,场景法的使用方法和运用场景
PDM fur
[untitled] open an item connection. If it cannot be displayed normally, Ping the IP address
Jupiter notebook common shortcut keys
As a programmer, how can we learn, grow and progress happily? (personal perception has nothing to do with technology)
[eosio] eos/wax signature error is_ Canonical (c): signature is not canonical
What is Unified Extensible Firmware Interface (UEFI)?