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:
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:
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
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:
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
You should read it
- String (String) in C / C ++
- Do you know what programming language is?
- String (String) in C #
- In the end, big universities realized that Java was a lousy language if used for introductory programming
- String (String) in PHP
- Overview of R language, install R on Windows and Linux
- Top website to write and compile online code best
- string.h in C
- The reason why C programming language is never outdated
- Why should you learn Python programming language?
- 16 programming languages will change your luck
- What is the first programming language in the world?
Maybe you are interested
How to know if your AirPods are charging? 5 ways to increase charging speed for your phone, both safe and easy to do Instructions on how to turn on battery saver feature right in the settings of the iPhone This technology can help fully charge an electric car's battery in just 10 minutes Use a battery charger like 'the beard that he has on her chin' Features that should be available on a 'trendy' laptop