for循环后加上:
if(i==n)
t[i] = 0;
这样修改,供参考:
void strmncpy(char *s, int m, int n, char *t)
{
/********* Begin ********/
int i;
for (i = 0; i < n; i++)
{
*(t + i) = *(s + m + i);
if (*(s + m + i) == '\0') break;
}
*(t + i) = '\0';
/******** End *********/
}
不知道你这个问题是否已经解决, 如果还没有解决的话:#include <stdio.h>
int Num(int num)
{
static int more = 0;
if (more == 1 && num == 1)
{//终止条件是more 和 num 都为 1
return 1;
}
if (more == 1)
{//如果是奇数 ,上次有一个空瓶没有兑换, 则需要在下一步减 1,不然会多加一瓶水
num = num - 1;
}
more = num % 2;//如果此次的水是奇数,则在下一个循环 num 需要减掉 1
return num + Num(num / 2 + more);
//加的 more 是没有兑换的空瓶 如果为 1,则需要在下一层减 1
}
int main()
{
int ret = Num(20);
//int ret = Num2(20);
printf("%d\n", ret);
return 0;
}
答案:
如果在字符串复制过程中出现乱码,可能是因为使用的复制方法存在问题。可以尝试使用更加稳定和正确的复制方法。以下是一些常用的字符串复制方法:
#include <stdio.h>
#include <string.h>
int main()
{
char source[] = "hello world";
char destination[20];
strcpy(destination, source); // 将source复制到destination中
printf("destination string is: %s\n", destination); // 输出结果为: hello world
return 0;
}
#include <stdio.h>
#include <string.h>
int main()
{
char source[] = "hello";
char destination[7];
strncpy(destination, source, sizeof(destination));
printf("destination string is: %s\n", destination);
return 0;
}
#include <stdio.h>
#include <string.h>
int main()
{
char source[] = "hello";
char destination[7];
memcpy(destination, source, sizeof(destination));
printf("destination string is: %s\n", destination);
return 0;
}
以上几种方法都可以避免字符串复制过程中出现乱码的情况。如果出现了乱码,应该检查代码中的错误,找到问题所在,并进行修复。