作业1:
输入一个字符串和一个正整数m,将该字符串中的前m个字符复制到另一个字符串中,再输出后一个字符串。
运行示例
Enter a string: abcdefghijk
Enter an integer: 6
The new string is abcdef
我的程序:
#include "stdafx.h"
#include "string.h"
#define N 80
void main()
{
char p1[N],p2[N];
int n,m;
printf("Enter a string:");
gets_s(p1,N);
printf("\n");
printf("Enter an integer:");
scanf_s("%d",&n);
printf("\n");
for(m=0;m<=(n-1);m++)
*(p2+m)=*(p1+m);
*(p2+m)='\0';
printf("The new string is :");
puts(p2);
}
其运行结果截图: