Prospect summary
- I want to use char type to store Chinese, and then print it out
Mode 1:
- Print in the form of char [] array. Then, because a man has two bytes, two% c are required for printing
example
#define MAXSIZE 20 int main() { char ch[MAXSIZE] = { "Zhao qiansun Li Zhou Wu Zheng Wang" }; int j = 1; for (int i = 0; i <= 14; i += 2) { printf("The first%d The last name is:%c%c\n", j++, ch[i], ch[i + 1]); } }
- Existing problems: the number of for loops must be the same as the number of elements and cannot be modified arbitrarily. Otherwise, the result will be garbled when printing
Figure I
- Solution: optimization
if (ch[i] =='\0') { break; }
**Complete**
#define MAXSIZE 20 int main() { char ch[MAXSIZE] = { "Zhao qiansun Li Zhou Wu Zheng Wang" }; int j = 1; for (int i = 0; i <= MAXSIZE; i += 2) { if (ch[i] =='\0') { break; } printf("The first%d The last name is:%c%c\n", j++, ch[i], ch[i + 1]); } }
Mode 2:
- Pointer mode rewriting
1. Array printing
example
char *ch2; char ch[MAXSIZE] = { "Zhao qiansun Li Zhou Wu Zheng Wang" }; ch2 = (char *)malloc(sizeof(char)); ch2 = ch; for (int i = 0; i <= MAXSIZE; i += 2) { if (ch2[i] =='\0') { break; } printf("The first%d The last name is:%c%c\n", j++, ch2[i], ch2[i + 1]); }
2. Pointer printing
example
char *ch2; char ch[MAXSIZE] = { "Zhao qiansun Li Zhou Wu Zheng Wang" }; ch2 = (char *)malloc(sizeof(char)); ch2 = ch; for (int i = 0; i <= MAXSIZE; i += 2) { if (*(ch2+i) =='\0') { break; } printf("The first%d The last name is:%c%c\n", j++, *(ch2+i), *(ch2 + i+1)); }
be careful
-
It cannot be written in the following form, because the character is one byte and the man is two. It cannot be output when written as follows
*(ch2 + 1) = 'Zhao'; *(ch2 + 2) = 'money'; *(ch2 + 3) = 'Sun'; *(ch2 + 4) = 'Lee'; *(ch2 + 5) = 'week'; *(ch2 + 6) = 'martial';
3. Optimize to while mode
example
int main() { char ch[MAXSIZE] = { "Zhao qiansun Li Zhou Wu Zheng Wang" }; int j = 1; char *ch2; ch2 = (char *)malloc(sizeof(char)); ch2 = ch; int i = 0; while (*(ch2 + i)!='\0') { printf("The first%d The last name is:%c%c\n", j++, *(ch2 + i), *(ch2 + i + 1)); i += 2; } }
Mode 3:
- Structure array overwrite
1. Use array mode in structure
example
#define MAXSIZE 20 typedef char ElementType; typedef int IntType; typedef struct SequenceList { // Elements of an array ElementType element[MAXSIZE]; // Length of array IntType intType[MAXSIZE]; }; int main() { SequenceList *p; int j = 1; int k = 0; char ch[20] = { "Zhao qiansun Li Zhou Wu Zheng Wang" }; int array[20] = { 31,33,35,37,39,41,43,45 }; p = (SequenceList*)malloc(sizeof(SequenceList)*MAXSIZE); if (p == NULL) { printf("error\n"); return 0; } for (int i = 0; i < MAXSIZE; i++) { p->element[i] = ch[i]; p->intType[i] = array[i]; } for (int i = 0; i <= MAXSIZE; i += 2) { if (p->element[i] == '\0') { break; } printf("The first%d The last name is:[00%d] = %c%c\n", j++, p->intType[k++], p->element[i], p->element[i + 1]); } }
2. Use array pointer mode in structure
(1) Basic writing method
example
typedef char ElementType; typedef struct SequenceListL { // Elements of an array ElementType *element; // Length of array int length; }; int main() { SequenceListL L; L.element = (ElementType*)malloc(sizeof(ElementType) * 10); if (L.element == NULL) { printf("error\n"); return 0; } char ch[20] = { "Zhao qiansun Li Zhou Wu Zheng Wang" }; for (int i = 0; i < 20; i++) { *(L.element + i) = ch[i]; } int j = 1; for (int i = 0; i <= 12; i += 2) { printf("The first%d The last name is:%c%c\n", j++, L.element[i], L.element[i + 1]); } }
(2) Upgrade the writing method, optimize the pointer, and remove a for loop
L.element = ch;
example
typedef char ElementType; typedef struct SequenceListL { // Elements of an array ElementType *element; // Length of array int length; }; int main() { SequenceListL L; int j = 1; L.element = (ElementType*)malloc(sizeof(ElementType) * 10); char ch[20] = { "Zhao qiansun Li Zhou Wu Zheng Wang" }; if (L.element == NULL) { printf("error\n"); return 0; } L.element = ch; for (int i = 0; i <= 12; i += 2) { printf("The first%d The last name is:%c%c\n", j++, L.element[i], L.element[i + 1]); } }
(3) Modify printing to pointer printing
example
typedef char ElementType; typedef struct SequenceListL { // Elements of an array ElementType *element; // Length of array int length; }; int main() { SequenceListL L; int j = 1; L.element = (ElementType*)malloc(sizeof(ElementType) * 10); char ch[20] = { "Zhao qiansun Li Zhou Wu Zheng Wang" }; if (L.element == NULL) { printf("error\n"); return 0; } L.element = ch; for (int i = 0; i <= 12; i += 2) { printf("The first%d The last name is:%c%c\n", j++, *(L.element + i), *(L.element + i + 1)); } }
(4) Optimize garbled code in printing results
-
When the number of for cycles changes, there will be garbled code
if (*(L.element + i) == '\0') { break; }
example
typedef char ElementType; typedef struct SequenceListL { // Elements of an array ElementType *element; // Length of array int length; }; int main() { SequenceListL L; int j = 1; int i = 0; L.element = (ElementType*)malloc(sizeof(ElementType) * 10); char ch[20] = { "Zhao qiansun Li Zhou Wu Zheng Wang" }; if (L.element == NULL) { printf("error\n"); return 0; } L.element = ch; for (int i = 0; i <= 20; i += 2) { if (*(L.element + i) == '\0') { break; } printf("The first%d The last name is:%c%c\n", j++, L.element[i], L.element[i + 1]); } }
(5) Optimize the cycle and reduce the number of cycles. At the same time, it can solve the problem of garbled code in the previous for
example
typedef char ElementType; typedef struct SequenceListL { // Elements of an array ElementType *element; // Length of array int length; }; int main() { SequenceListL L; int j = 1; int i = 0; L.element = (ElementType*)malloc(sizeof(ElementType) * 10); char ch[20] = { "Zhao qiansun Li Zhou Wu Zheng Wang" }; if (L.element == NULL) { printf("error\n"); return 0; } L.element = ch; while (*(L.element + i) != '\0') { printf("The first%d The last name is:%c%c\n", j++, *(L.element + i), *(L.element + i + 1)); i += 2; } }
summary
- The method of printing Chinese characters is indeed much more complicated than the simple output of 'a','b''c', and I think of many situations
- There are indeed many problems with the structure pointer, which are constantly improving
- The garbled Chinese character filtering method has really been thinking for a long time. It is different from the previous filtering method. Please see the previous method How can c language avoid printing empty data?
- This problem encountered by our blogger can help you. If you like, please pay attention, like and collect