Table of Contents
This guide covers string in c with practical context and easy-to-follow details. Use it to understand the subject and apply the information confidently.
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
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:
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
FAQ
What should I check before following these steps?
Confirm device and software compatibility, save important data, and make sure you have the required permissions, files, and account access.
Why might the process not work?
Common causes include outdated software, missing permissions, incompatible hardware, an unstable connection, or completing a step in the wrong order.
Can I undo the changes if necessary?
That depends on the tool or setting. Use built-in restore options when available, keep a backup, and record the original configuration first.
Reader Comments 0
Sign in with email or Google to join the discussion.