String in C

The string in the C programming language is essentially a one-dimensional array of characters that ends with a null character ''.

The string in the C programming language is essentially a one-dimensional array of characters that ends with a null character '' .

The declaration and initialization section below creates a string that includes a word "Hello". To keep null values ​​at the end of the array, the size of the array of characters includes a more string than the number of characters in the "Hello" keyword.

 char loichao [ 6 ] = { 'H' , 'e' , 'l' , 'l' , 'o' , '' }; 

If you follow the rules for initializing strings, you can write the following command:

 char loichao [] = "Hello" ; 

Here is the memory cell representation for the above string segment in C / C ++ language:

String in C Picture 1String in C Picture 1

In fact, you do not put the null character at the last position of the constant variable. The C compiler automatically adds '' at the last location of the string when it initializes the string. Try the example to print the following string:

 #include int main () { char loichao [ 6 ] = { 'H' , 'e' , 'l' , 'l' , 'o' , '' }; printf ( "Khi gap nhau, ban chao: %sn" , loichao ); printf ( "n===========================n" ); printf ( "QTM chuc cac ban hoc tot! n" ); return 0 ; } 

Compiling and running the above C program will result:

String in C Picture 2String in C Picture 2

C language supports a wide range of functions to manipulate ending strings to be null:

Function Purpose strcpy (s1, s2);

Copy the string s2 for the string s1.

strcat (s1, s2);

Connect string s2 at the end of string s1.

strlen (s1);

Returns the length of the string s1.

strcmp (s1, s2);

Returns 0 if s1 and s2 are the same; less than 0 if s1 s2.

strchr (s1, ch);

Returns the pointer to the first position of ch in s1.

strstr (s1, s2);

Returns the pointer to the first position of the string s2 in the string s1.

Here is an example for using some of the above functions:

 #include #include /* thu vien cho cac ham xu ly chuoi*/ int main () { char chuoi1 [ 12 ] = "Hello" ; char chuoi2 [ 12 ] = "QTM" ; char chuoi3 [ 12 ]; int dodai ; /* sao chep chuoi1 vao trong chuoi3 */ strcpy ( chuoi3 , chuoi1 ); printf ( "Ban su dung ham strcpy( chuoi3, chuoi1) de sao chep: %sn" , chuoi3 ); /* noi hai chuoi: chuoi1 va chuoi2 */ strcat ( chuoi1 , chuoi2 ); printf ( "Ban su dung ham strcat( chuoi1, chuoi2) de noi chuoi: %sn" , chuoi1 ); /* tinh do dai cua chuoi1 sau khi noi chuoi */
/* TipsMake.com */ dodai = strlen ( chuoi1 ); printf ( "Ban su dung ham strlen(chuoi1) de tinh do dai: %dn" , dodai ); printf ( "n===========================n" ); printf ( "QTM chuc cac ban hoc tot! n" ); return 0 ; }

Compiling and running the above C program will result:

String in C Picture 3String in C Picture 3

You can find a complete list of functions related to strings in the Standard C Library.

According to Tutorialspoint

Previous article: Cursor in C

Next lesson: Structure (Struct) in C

4 ★ | 2 Vote