At all Enter made in c we"ve become familiar with how to lớn declare, entry & string in C simply. In this article we will talk more about the examples, homework chain .
Bạn đang xem: Chuỗi ký tự trong c
1. Overview
The string is treated as an array 1 dimensions of elements of type char as characters, numbers & any special characters such as +, -, *, /, $, #,...By convention, a sequence will be terminated by the null character (‘ ’ : Register turong).Example: string "Infoworld" stored as follows:
How to lớn store string
2. Some declaring, initialization string
We consider the following example:
// e.g about string - code by nguyenvanquan7826#include int main() // khai bao chuoi teo toi domain authority 50 ky tu char name<50>; printf("Hi, What is your name? My name is: "); gets(name); printf("Hi %s, welcome khổng lồ C language ", name); // khoi tao chuoi ngay trong khi khai bao char myLove<> = "Nguyen Thi Lap Lanh"; puts(myLove); return 0;Result:
Hi, What is your name? My name is: Nguyen Van quan lại Hi Nguyen Van Quan, welcome lớn C language Nguyen Thi sparkle
In the program on, I have used the function puts lớn print out the string Mylove, this is also a function to đầu ra string.
As above we can see is the declaration chain or medium chain then import declaration assigning values to the string just right. However we can not declare then assign values as follows:
char name<50>;name = "Nguyen Van Quan"; // errorWhere want to lớn declare then assign values, we must use the string copy function strcpy in library string.h to lớn save the values as follows:
// e.g about string - code by nguyenvanquan7826#include #include int main() char name<50>; strcpy(name, "Nguyen Van Quan"); puts(name); return 0;
3. Some examples of strings in C
3.1 Example 1: Count the number of words in the string
Please enter a string & count the number of words in the string. VD chain “Nguyen Van Quan” have 3 from.To vì this, we see each of the characters consecutively và diverging words with spaces. Therefore this article will be provided khổng lồ count the number of spaces in the string. If string 1 word, no spaces, strings 2 from there 1 spaces between 2 from that. N general is from, there will be n-1 spaces.
The next problem is how to count the spaces? Simple, It has been known as the first xuất hiện string is an array of characters, so we can browse the characters turn to lớn examining the chain of characters are spaces. But want khổng lồ browse all the characters in the string (character array) they must know the number of characters in the string (number of elements in the array).
Fortunately, in the library string.h we have a function khổng lồ get the length of the string as a function strlen. (str – string, only – length).
// e.g about string - code by nguyenvanquan7826#include #include // for strlen functionint main() { char s<50>; printf("Enter a string: "); gets(s); int i, count = 0; // count - bien dem so luong dau cach for (i = 0; i Pretty simple code, you read, understand and test nhé.
Yet we have a code on the note:
Because the string is an array of characters, should like to take ith character in the string s, then we as the array access is sint len = strlen(s);for (i = 0; i
3.2 Example 2: Standardize the string
Please enter a string & remove all extra spaces at the top, Last & between words if.Xem thêm: Dụng Cụ Tập Lắc Eo Giá Sỉ, Bán Vòng Lắc Eo Tphcm, Vòng Lắc Eo Bằng Mây Tphcm Giá Rẻ
This problem is important for the problem of software, later when stored, đầu vào data is noteworthy lớn be standardized, no surplus or deficit caused errors in the process & seek.
The first character of the string s is space, then s<0> the spaces, we delete it as done.The characters between words if redundant ie sThe next problem is how to lớn delete 1 characters in the string? You look at the example above we use the string copy function, & to delete 1 or a number of characters in the string we will use this function, but will be used in the copy address.
To delete a character from i to lớn j characters in the string s, we command strcpy(&s, &s
// e.g about string - code by nguyenvanquan7826#include #include int main() { char s<50>; printf("Enter a string: "); gets(s); // delete all space at start of string while( s<0> == ' ' ) strcpy(&s<0>, &s<1>); // delete all space at over of string while( s< strlen(s)-1 > == ' ') s< strlen(s)-1 > = '