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 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 #
- 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?
May be interested
- How to Manipulate Strings in Javastrings are sequences of characters. for example, 'hello!' is a string because it is made up of the characters 'h', 'e', 'l', 'l', 'o', and '!'. in java, strings are objects, which means that there is a string class that has fields and...
- How to insert characters at the beginning or end of a string in Excelrecently software tips received many questions asking how to insert characters at the beginning or end of a string in excel. so today, dexterity software introduces readers how to insert more characters into the string in this article, invite you to track.
- Write a program to reverse a string in Pythonwhat is string reversal in python? how to use the string reverse function in python? let's find out with tipsmake.com.com!
- The LEFT function, how to use the left-hand string cutting function in Excelthe left function in excel is used to trim the left string of characters in data parameters.
- Make changes in Gitjerry creates a copy of the repository on his machine and decides to perform basic operations. so he created the file string.c. after adding content, string.c will look like s
- Who would have thought that the production process of the familiar familiar string was complicated and such a feata familiar string, items that we still use every day to tie and pack goods. its presence is considered by everyone to be obvious, but most people don't know how the string is produced.
- How to use ConcateNate function on Excelconcatenate function on excel is a function that connects strings of characters in different data columns in a table, forming a complete string of characters.
- Analysis of an attack (Part 2)we have already introduced you in a section of observable information while opening the package string sent by nmap. the sent string starts with an icmp echo response to determine if the computer or network has been assigned to the ip address. add v & agr
- Python data type: string, number, list, tuple, set and dictionaryin this section, you'll learn how to use python as a computer, grasp python's data types and take the first step towards python programming.
- CONCATENATE function (string concatenation function) in Exceluse the concatenate function when you want to concatenate existing text strings into a single, complete text string. for the syntax and usage of the concatenate function, follow the article below.