当前位置:网站首页>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 :

  1. '\063' It means the character '3', because '3' Of ASCII Code is 30( Hexadecimal ),48( Decimal system ),63( octal ).
  2. '\x41' It means the character 'A', because 'A' Of ASCII Code is 41( Hexadecimal ),65( Decimal system ),101( octal ).

 

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);

}

 

 

原网站

版权声明
本文为[Soy sauce;]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206252010036426.html