当前位置:网站首页>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); } |
边栏推荐
- 电路模块分析练习5(电源)
- Pit resolution encountered using East OCR (compile LAMS)
- [untitled] open an item connection. If it cannot be displayed normally, Ping the IP address
- 【2023校招刷题】番外篇1:度量科技FPGA岗(大致解析版)
- Utilisation de la classe Ping d'Unity
- 毕业旅行 | 伦敦5日游行程推荐
- PDM fur
- Es6-- set
- Implementation of importing vscode from PDM
- Es7/es9 -- new features and regularities
猜你喜欢
问题记录与思考
Paper notes: multi tag learning MSWl
Oracle -- table operation
After xampp restarts, the MySQL service cannot be started.
ES6 const constants and array deconstruction
【无标题】打开一个项目连接,无法正常显示时,ping一下ip
Live800 online customer service system: do business across time and space, starting from each interaction
Es7/es9 -- new features and regularities
Use and difference between ue4\ue5 blueprint node delay and retroggable delay
UE4_UE5結合offline voice recognition插件做語音識別功能
随机推荐
As a programmer, how can we learn, grow and progress happily? (personal perception has nothing to do with technology)
小程序绘制一个简单的饼图
Unity technical manual - particle foundation main module attributes - upper
The sum of logarithms in group 52--e of Niuke Xiaobai monthly race (two points)
Idea shortcut
信息学奥赛一本通 1353:表达式括号匹配(stack) | 洛谷 P1739 表达式括号匹配
记一次beego通过go get命令后找不到bee.exe的坑
#23class介绍
Technology blog site collection
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
ES6 const constants and array deconstruction
统计字符串中不同回文子序列的个数
问题记录与思考
元宇宙标准论坛成立
Basic operator
Unity technical manual - life cycle rotation rotationoverlifetime- speed rotation rotationbyspeed- and external forces
Multithreaded learning 2- call control
UE4_ Ue5 combines the offline voice recognition plug-in for speech recognition
【opencv450-samples】inpaint 使用区域邻域恢复图像中的选定区域
Sword finger offer 46 Translate numbers to strings (DP)