#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define SIZE 20
char * funcpy(char s1[],char s2[],int n);
int main(void){
char s1[SIZE];
char s2[SIZE];
int n=0;
puts("Input a string:");
gets(s2);
puts("How many chars would you like to copy:");
scanf("%d",&n);
puts("Copy finished:");
puts(funcpy(s1,s2,n));
return 0;
}
char * funcpy(char s1[],char s2[],int n){
int i;
if(n<strlen(s2)){
for(i=0;i<n;i++){
s1[i]=s2[i];
}
s1[i+1]='\0';
return s1;
}else{
for(i=0;i<n;i++){
if(i<strlen(s2)){
s1[i]=s2[i];
}else{
s1[i]='\0';
}
}
return s1;
}
}
本文转hackfreer51CTO博客,原文链接:http://blog.51cto.com/pnig0s1992/428597,如需转载请自行联系原作者