为啥输入截取两个但却截出了三个字
#include <stdio.h>#define MAX 1024
int main() {
char str1;
char str2;
char *target1 = str1;
char *target2 = str2;
char ch;
int n;
printf("请输入一个字符串到 str1 中:");
fgets(str1, MAX, stdin);
printf("请输入需要拷贝的字符个数:");
scanf("%d", &n);
printf("开始拷贝 str1 的内容到 str2 中...\n");
while (n-->0) {
ch = *target2++ = *target1++;
if (ch == '\0') {
break;
}
if ((int)ch < 0) {
*target2++ = *target1++;
*target2++ = *target1++;
}
}
*target2 = '\0';
printf("拷贝完毕!\n");
printf("现在,str2 中的内容是:%s\n", str2);
return 0;
}
Linux 采用 UTF-8 编码,每个汉字占用 3 个字节
while (n-->0) {
ch = *target2++ = *target1++;
if (ch == '\0') {
break;
}
if ((int)ch < 0) {
*target2++ = *target1++;
*target2++ = *target1++;
}
}
但是,你的系统是 Windows,中文 Windows 采用 GBK 编码,每个汉字占用 2 个字节,所以,上面的代码必须修改为:
while (n-->0) {
ch = *target2++ = *target1++;
if (ch == '\0') {
break;
}
if ((int)ch < 0) {
*target2++ = *target1++;
}
}
页:
[1]