当前位置:网站首页>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); } |
边栏推荐
- Flex & Bison 開始
- 百度:2022年十大热度攀升专业出炉,第一名无悬念!
- Multithreaded learning 1
- Kubernetes cluster construction of multiple ECS
- Idea auto generator generates constructor get/set methods, etc
- 牛客小白月赛52--E 分组求对数和(二分)
- 为什么OpenCV计算的帧率是错误的?
- 多模态数据也能进行MAE?伯克利&谷歌提出M3AE,在图像和文本数据上进行MAE!最优掩蔽率可达75%,显著高于BERT的15%...
- Multithreaded learning 2- call control
- Unity technical manual - particle foundation main module attributes - upper
猜你喜欢

Utilisation de la classe Ping d'Unity

Pit resolution encountered using East OCR (compile LAMS)

konva系列教程2:绘制图形

STM32开发板+机智云AIoT+家庭监测控制系统

The first public available pytorch version alphafold2 is reproduced, and Columbia University is open source openfold, with more than 1000 stars

STM32 development board + smart cloud aiot+ home monitoring and control system

Problem recording and thinking

Set up your own website (15)
[email protected]@COLLATION_ CONNECTION */"/>. SQL database import error: / *! 40101 SET @OLD_ COLLATION_ [email protected]@COLLATION_ CONNECTION */

Xampp重启后,MySQL服务就启动不了。
随机推荐
Unity technical manual - color in life cycle coloroverlifetime-- speed color colorbyspeed
ACM. HJ16 购物单 ●●
【AXI】解读AXI协议原子化访问
[untitled] open an item connection. If it cannot be displayed normally, Ping the IP address
记一次beego通过go get命令后找不到bee.exe的坑
UE4_UE5结合offline voice recognition插件做语音识别功能
Unity technical manual - life cycle rotation rotationoverlifetime- speed rotation rotationbyspeed- and external forces
zabbix_server配置文件详解
The new version of Tencent's "peace elite" is coming: add a new account security protection system, and upgrade the detection of in-game violations
konva系列教程2:绘制图形
Jupiter notebook common shortcut keys
UE4\UE5 蓝图节点Delay与Retriggerable Delay的使用与区别
Idea shortcut
Idea common plug-ins
[opencv450 samples] read the image path list and maintain the proportional display
Oracle - data query
Baidu: in 2022, the top ten hot spots will rise and the profession will be released. There is no suspense about the first place!
问题记录与思考
Day3 data types and operators summary and job
元宇宙标准论坛成立