C語言中如何輸出漢字;如何用C語言漢字編碼輸出漢字(超全版)
目錄
前景提要
- 想用char類型存儲中文,然後列印出來
方式一:
- 使用char [] 數組的方式列印,然後,因為一個漢子兩個位元組,所以,列印時候,需要兩個%c
實例
#define MAXSIZE 20
int main()
{
char ch[MAXSIZE] = { "趙錢孫李周吳鄭王" };
int j = 1;
for (int i = 0; i <= 14; i += 2) {
printf("第%d個姓氏是:%c%c\n", j++, ch[i], ch[i + 1]);
}
}
- 存在問題: for循環的次數必須寫死跟元素的個數一樣,不能隨便修改,否則,列印的時候,結果就會有亂碼的情況.
圖一
- 解決方法:優化
if (ch[i] =='\0')
{
break;
}
** 完整**
#define MAXSIZE 20
int main()
{
char ch[MAXSIZE] = { "趙錢孫李周吳鄭王" };
int j = 1;
for (int i = 0; i <= MAXSIZE; i += 2) {
if (ch[i] =='\0')
{
break;
}
printf("第%d個姓氏是:%c%c\n", j++, ch[i], ch[i + 1]);
}
}
方式二:
- 指針方式改寫
1. 數組方式列印
實例
char *ch2;
char ch[MAXSIZE] = { "趙錢孫李周吳鄭王" };
ch2 = (char *)malloc(sizeof(char));
ch2 = ch;
for (int i = 0; i <= MAXSIZE; i += 2) {
if (ch2[i] =='\0')
{
break;
}
printf("第%d個姓氏是:%c%c\n", j++, ch2[i], ch2[i + 1]);
}
2. 指針方式列印
實例
char *ch2;
char ch[MAXSIZE] = { "趙錢孫李周吳鄭王" };
ch2 = (char *)malloc(sizeof(char));
ch2 = ch;
for (int i = 0; i <= MAXSIZE; i += 2) {
if (*(ch2+i) =='\0')
{
break;
}
printf("第%d個姓氏是:%c%c\n", j++, *(ch2+i), *(ch2 + i+1));
}
注意
-
不能寫成如下形式,因為字元是一個位元組,而漢子是兩個,寫成如下是無法輸出的.
*(ch2 + 1) = '趙'; *(ch2 + 2) = '錢'; *(ch2 + 3) = '孫'; *(ch2 + 4) = '李'; *(ch2 + 5) = '周'; *(ch2 + 6) = '武';
3. 優化為while方式
實例
int main()
{
char ch[MAXSIZE] = { "趙錢孫李周吳鄭王" };
int j = 1;
char *ch2;
ch2 = (char *)malloc(sizeof(char));
ch2 = ch;
int i = 0;
while (*(ch2 + i)!='\0')
{
printf("第%d個姓氏是:%c%c\n", j++, *(ch2 + i), *(ch2 + i + 1));
i += 2;
}
}
方式三:
- 結構體數組改寫
1. 使用結構體內數組方式
實例
#define MAXSIZE 20
typedef char ElementType;
typedef int IntType;
typedef struct SequenceList {
// 數組的元素
ElementType element[MAXSIZE];
// 數組的長度
IntType intType[MAXSIZE];
};
int main()
{
SequenceList *p;
int j = 1;
int k = 0;
char ch[20] = { "趙錢孫李周吳鄭王" };
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("第%d個姓氏是:[00%d] = %c%c\n", j++, p->intType[k++], p->element[i], p->element[i + 1]);
}
}
2. 使用結構體內數組指針方式
(1) 基礎寫法
實例
typedef char ElementType;
typedef struct SequenceListL {
// 數組的元素
ElementType *element;
// 數組的長度
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] = { "趙錢孫李周吳鄭王" };
for (int i = 0; i < 20; i++)
{
*(L.element + i) = ch[i];
}
int j = 1;
for (int i = 0; i <= 12; i += 2) {
printf("第%d個姓氏是:%c%c\n", j++, L.element[i], L.element[i + 1]);
}
}
(2) 升級寫法,指針的優化,去除一個for循環
L.element = ch;
實例
typedef char ElementType;
typedef struct SequenceListL {
// 數組的元素
ElementType *element;
// 數組的長度
int length;
};
int main()
{
SequenceListL L;
int j = 1;
L.element = (ElementType*)malloc(sizeof(ElementType) * 10);
char ch[20] = { "趙錢孫李周吳鄭王" };
if (L.element == NULL)
{
printf("error\n");
return 0;
}
L.element = ch;
for (int i = 0; i <= 12; i += 2) {
printf("第%d個姓氏是:%c%c\n", j++, L.element[i], L.element[i + 1]);
}
}
(3) 修改列印為指針列印
實例
typedef char ElementType;
typedef struct SequenceListL {
// 數組的元素
ElementType *element;
// 數組的長度
int length;
};
int main()
{
SequenceListL L;
int j = 1;
L.element = (ElementType*)malloc(sizeof(ElementType) * 10);
char ch[20] = { "趙錢孫李周吳鄭王" };
if (L.element == NULL)
{
printf("error\n");
return 0;
}
L.element = ch;
for (int i = 0; i <= 12; i += 2) {
printf("第%d個姓氏是:%c%c\n", j++, *(L.element + i), *(L.element + i + 1));
}
}
(4) 優化列印結果中的亂碼
-
for循環次數變化的時候,會出現亂碼
if (*(L.element + i) == '\0') { break; }
實例
typedef char ElementType;
typedef struct SequenceListL {
// 數組的元素
ElementType *element;
// 數組的長度
int length;
};
int main()
{
SequenceListL L;
int j = 1;
int i = 0;
L.element = (ElementType*)malloc(sizeof(ElementType) * 10);
char ch[20] = { "趙錢孫李周吳鄭王" };
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("第%d個姓氏是:%c%c\n", j++, L.element[i], L.element[i + 1]);
}
}
(5) 優化循環,減少循環次數,同時,可以很好的解決上一個for存在的亂碼問題.
實例
typedef char ElementType;
typedef struct SequenceListL {
// 數組的元素
ElementType *element;
// 數組的長度
int length;
};
int main()
{
SequenceListL L;
int j = 1;
int i = 0;
L.element = (ElementType*)malloc(sizeof(ElementType) * 10);
char ch[20] = { "趙錢孫李周吳鄭王" };
if (L.element == NULL)
{
printf("error\n");
return 0;
}
L.element = ch;
while (*(L.element + i) != '\0') {
printf("第%d個姓氏是:%c%c\n", j++, *(L.element + i), *(L.element + i + 1));
i += 2;
}
}
總結
- 列印漢字的方法確實比簡單的輸出’a’,’b”c’複雜了很多,想到很多情況
- 結構體指針這裡確實有很多問題,不斷的在改進
- 漢字的亂碼過濾方式確實想了很久,跟之前的過濾方式不同,之前的方式請看c語言怎麼避免列印空數據?
- 本部落客遇到的這個問題能幫助到你,喜歡的話,請關注,點贊,收藏.