实现 strncpy 函数,让用户输入需要拷贝的字符个数。但是输入中文以后结果不正确
#define MAX 1024int main()
{
int i = 0,num,mum=0,mm=0;
char str1;
char str2;
char* m = str1;
char* n = str2;
printf("input a string\n");
fgets(str1, MAX, stdin);
printf("number\n");
scanf("%d", &num);
while(num--)
{
if (*m++ >= 0)
{
mum++;
}
else if(*m++<0){
mm++;
}
}
str1 = '\0';
while ((*n++=*m++) != '\0')
{
;
}
printf("%s", str2);
return 0;
}
#define MAX 1024
int test10() {
int i = 0, num, mum = 0, mm = 0;
char str1;
char str2;
char* m = str1;
char* n = str2;
printf("input a string\n");
fgets(str1, MAX, stdin);
printf("string is %s\n",str1);
printf("number\n");
scanf("%d", &num);
while (num--)
{
*n++ = *m++;
}
*n = '\0';
printf("%s", str2);
return 0;
}
我建议直接改成这样子,前提是默认你知道自己要取几位,比如wo我们,你知道 wo我=4个长度(中文是一个字占两个字符的)
页:
[1]