C language character, character array, character pointer

definition

(1) String

A string is a string of characters composed of letters, numbers, underscores, spaces and other characters. It is a constant. (however, C language does not provide string data type, which is generally represented by character array)

(2) Character array

Each character array element is a single character array.

initialization

(1) Character

char a = 'a';//Only one character can be stored

(2) Character array

char str[] = { "abcd" };//Curly braces can be omitted.
char str[] = "abcd";
char str[] = { 'a', 'b', 'c', 'd' }
char str[5];//If no initial value is assigned to the right, the array size must be indicated.
char str[5]="abcd";//The reason why the length here is 5 instead of 4 is that there is a '\ 0' after the C language string by default. If you don't believe it, you can change it to 4 or use sizeof() to check the size.
char str[4] = { 'a', 'b', 'c', 'd' }//Here, the length of the array is 4, because it is the initial value of one character, so there is no '\ 0' occupation, so the actual size is 4 instead of 5.

The following initialization is the wrong way:

char str[5];
str[]="abcd";//C language does not provide operators that can directly operate on strings; "=" can be used for assignment of other data types, but cannot be directly assigned to strings.

(3) Character pointer

//Pointers generally need to open up their own space before they can be used normally. The following types depend on the constant storage area of the system.
char *str;
//perhaps
char *str = "abcde"; //This initialization process is to point the pointer str to the first address of the string instead of passing the value of the string, so you can.
//That's OK
char *str;
str="abcde" ;
//This one won't work
char *str;
*str="abcde" ;//Wrong! Because the string constant passes its first address, not a specific value.

Supplement:
For pointer assignment, the left operand of "=" can be * STR or str.

When the left operand of "=" is * str, the data stored in the address pointed to by str is changed;

When the left operand of "=" is str, the address pointed to by str is changed.

Assignment and modification

(1) Character

This is relatively simple. Just change it directly

char a;
a='a';
a='b';

(2) Character array

1. Single character assignment can refer to array elements by array subscript or pointer.

 //The array element can be referenced through the array subscript to complete the assignment
char str[5];
char ch='a';
for(int i=0;i<5;i++ )
str[i]=ch+i ;//Here 'a' will be converted into Ascll code for addition operation, and then become a new character. For example, 'a'+1 will become 'b'
//You can also assign a pointer to the corresponding element of the array
char *p;
for( p=str; p<str+5; p++ )//Because the array is a continuous space, the pointer address of str can be assigned to p here, and P + + can be used to realize address self increment.
*p=ch++;

2. String can be used for string assignment The string operation function in the H header file is used for assignment.

char str[5];
strcpy(str, "abcd");

Note: the modification is similar to the array.

(3) Character pointer

1. Direct assignment

str="abcde";

Note: in this case, the value inside cannot be modified.
2. Apply for space with malloc. This function is in stdlib H header file.

char *str;
str=(char *)malloc(sizeof(char)*5);//Apply for 5 spaces of char type size
//In this way, it can be assigned and modified like a character array.

correlation function

1, C language is used to obtain the function of user input from the keyboard

(1) Scanf (format control string ", address table column)

//You can enter multiple types of data.

char a;
char str1[5];
char *str2;
str2=(char *)malloc(sizeof(char)*5);
scanf("%c",&a);//Get a character
scanf("%s",str1);//Get a string
scanf("%s",str2);

(2)getchar()

//Used to get a single character

char ch;
ch = getchar();

Note: you can also use getche() or getch() instead of getchar(). Its function is to read a character from the keyboard (without pressing enter). Note that the header file < conio h>.

(3)gets(char *str)

//Get a row of data and process it as a string.

Note: spaces in the string can also be entered directly. In addition, the read newline character will be replaced by the end of the string.

char str1[5];
char *str2;
str2=(char *)malloc(sizeof(char)*5);
gets(str1);//Parameter pointer address
gets(str2);

2, C language is a function for printing strings to the screen

(1) Printf (format control string ", address table column)

//You can input and print various types of data.

char a='a';
char str[]="abcd";
printf("%c",a);//Print a single character
printf("%s",str);//Print string (Note: the end of the printed string must end with '\ 0', otherwise an error will occur!!)

(2)puts(const char *string)

//Output the string in a single line, that is, after outputting the string content, a new line will be added at the end automatically.

char str[]="abcd";;
puts(str);//Parameter pointer address

3, C language header file string Common string processing library functions provided by H

(1) strlen( char * str )

//Return string length, excluding '\ 0' at the end;

char str[]="abcde";
printf("%d",strlen(str));//The result is 5;

(2)strcmp(char * str1,char * str2 )

//Compare two string sizes

//Start the two strings from the initial letter and compare them one by one according to ASCII code, and the comparison result returns an integer.

Note: if str1==str2, it returns zero; If STR1 < STR2, a negative number is returned; If STR1 > STR2, it returns a positive number (if the character length is different, the longer the character length is greater).

char str1[]="abcde";
char str2[]="abcde";
printf("%d",strcmp(str1,str2));//The result is 0.
char str1[]="abcde";
char str2[]="abcdf";//Because the Ascll code of e is smaller than f
printf("%d",strcmp(str1,str2));//The result is negative.
char str1[]="abcdf";
char str2[]="abcde";
printf("%d",strcmp(str1,str2));//The result is a positive number.
char str1[]="abcde";
char str2[]="abcd";
printf("%d",strcmp(str1,str2));//The result is a positive number.

(3)strcpy( char * str1,char * str2 )

//Copy the contents of str2 into str1, including the end of string flag (the contents of str2 remain unchanged).
Note: the essence is to copy the string starting from str2 address to the address space starting from str1.

char str1[5];
char str2[]="abcd";
strcpy(str1, str2);
//You can do the same
char str[5];
strcpy(str1, "abcd");//Because "abcd" is also equivalent to returning an address.

(4)strcat( char * str1,char * str2 )

Function: add string str2 to the end of string str1 to form a new string, and '\ 0' after str1 will be cancelled.

Note: you must ensure that the space of string str1 is large enough, otherwise an error will occur.

char str1[10]="abc";
char str2[]="defg";;
strcat(str1, str2);//As a result, str1 becomes "ABCDEFG" and STR2 remains unchanged.

(5)strchr( char * str1,char str2 )

Function: return pointer to the first address of str2 in str1. (for characters)

char str1[10]="abcdefg";
char str2='d';
 char *p;
    p = strchr(str1, str2);//str2 can only be characters
    printf("%d\n", str1);//result; six million three hundred and fifty-six thousand seven hundred and eighteen
    printf("%d\n", p);//Results: 6356721 is the address of str1 plus the subscript of str2 appearing for the first time in str1
    printf("%d\n", p-str1);//Results: 3 is the subscript of str2 in str1 for the first time
	

(6)strstr( char * str1,char str2 )

Function: return pointer to the first address of str2 in str1. (for strings)

char str1[10]="abcdefgde";
char str2[]="de";
 char *p;
    p = strstr(str1, str2);str2 Is a string
    printf("%d\n", str1);//Result: 6356722
    printf("%d\n", p);//Result: 6356725
    printf("%d\n", p-str1);//Result: 3

To be continued.....

Keywords: string

Added by RobReid on Sat, 12 Feb 2022 05:15:02 +0200