目录
前景提要
- 想用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语言怎么避免打印空数据?
- 本博主遇到的这个问题能帮助到你,喜欢的话,请关注,点赞,收藏.