C语言中如何输出汉字;如何用C语言汉字编码输出汉字(超全版)
目录
前景提要
方式一:
方式二:
1. 数组方式打印
2. 指针方式打印
3. 优化为while方式
方式三:
1. 使用结构体内数组方式
2. 使用结构体内数组指针方式
(1) 基础写法
(2) 升级写法,指针的优化,去除一个for循环
总结
前景提要
想用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语言怎么避免打印空数据?
本博主遇到的这个问题能帮助到你,喜欢的话,请关注,点赞,收藏.
C语言中如何输出汉字;如何用C语言汉字编码输出汉字(超全版)的更多相关文章
C语言中的位操作(12)--判断一个数字是否包含一个全零字节
本文主要介绍一系列算法,算法主要功能是判断一个数字(二进制)中是否包含全零字节 e.g.1010 1111 0000 0000 1001 1111 0001 1111 即 32位整数:A4A3A2A1 ...
C#将dataGridView中显示的数据导出到Excel(大数据量超有用版)
开发中非常多情况下须要将dataGridView控件中显示的数据结果以Excel或者Word的形式导出来,本例就来实现这个功能. 因为从数据库中查找出某些数据列可能不是必需显示出来,在dataGrid ...
IDEA中web项目打成war包并在本地tomcat部署(超细版)
准备工作:相关软件及插件IDEA(2021.1.3).tomcat(8.5.50)且在IDEA中调用tomcat运行时没有任何错误的,如何下载安装百度都有详细的介绍,这里就不过多的介绍了,版本不同操作 ...
C语言中格式字符串
C语言中格式字符串的一般形式为: %[标志][输出最小宽度][.精度][长度]类型, 其中方括号[]中的项为可选项. 一.类型 我们用一定的字符用以表示输出数据的类型,其格式符和意义下表所示: 字符 ...
C语言中字符串的格式化
本文整理转载自:http://wenku.baidu.com/view/065d62fff705cc1755270989.html C语言中格式字符串的一般形式为: %[标志][输出最小宽度][.精度 ...
c语言中类型转换与赋值运算符、算术运算符、关系运算符、逻辑运算符。原码、反码、补码。小解。
类型转换 自动转换 小范围的类型能够自动转换成大范围的类型.short->int->long->float->double 强制类型转换 (类型名)变量或数值 #include ...
这样子来理解C语言中指针的指针
友情提示:阅读本文前,请先参考我的之前的文章<从四个属性的角度来理解C语言的指针也许会更好理解>,若已阅读,请继续往下看. 我从4个属性的角度来总结了C语言中的指针概念.对于C语言的一个指 ...
C语言中关于scanf函数的用法
scanf()函数的控制串 函数名: scanf 功 能: 执行格式化输入 用 法: int scanf(char *format[,argument,...]); scanf()函数是通用终端格式化 ...
C语言中static的作用及C语言中使用静态函数有何好处
转自:http://www.jb51.net/article/74830.htm 在C语言中,static的作用有三条:一是隐藏功能,二是保持持久性功能,三是默认初始化为0. 在C语言中,static ...
随机推荐
学Go语言能找到实习吗,年前聊聊Go和Java
前言 快过年了,来公司的人越来越少,估计明天都没什么人了,白泽也要收拾收拾回老家过年了.今天就随便写写零碎的事,所以行文当中难免思路跳跃,请大家一笑了之. 每次冷不丁收到公司给发的礼品袋,心头总是莫名 ...
service层 必须做业务逻辑的处理
package com.aaa.zxf.service; import com.aaa.zxf.mapper.BookMapper; import com.aaa.zxf.model.Book; im ...
C++的set重载运算符
转载: https://www.cnblogs.com/zhihaospace/p/12843802.html set 容器模版需要3个泛型参数,如下: template Redis设计与实现 简述Redis设计与实现 Redis是一个高性能的key-value的非关系型数据库,Redis是运行在内存中的一种数据库,但是它也可以持久化到磁盘中,Redis的实现有着更为复杂的数据结构并且提供对 ... bom-offset k8s 通过helm发布应用 什么是helm? Helm 是 Kubernetes 的包管理器.Helm 是查找.分享和使用软件构建 Kubernetes 的最优方式. 在红帽系的Linux中我们使用yum来管理RPM包,类似的, ...