DIY Func same as strncpy()

简介:

#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,如需转载请自行联系原作者
相关文章
|
1天前
strlen,strcpy,stract,strcmp,strstr函数的模拟实现
strlen,strcpy,stract,strcmp,strstr函数的模拟实现
25 3
|
1天前
|
安全 C语言
snprintf的用法
简要介绍了snprintf的常用方法,能大大的简化我们的代码
|
9月前
strcpy与strncpy的模拟与实现
strcpy与strncpy的模拟与实现
26 0
|
7月前
|
存储 Linux C语言
深入解析Linux环境下的sprintf()和printf()函数
在C语言中,`sprintf()`和`printf()`函数是用于格式化输出的两个重要函数。`sprintf()`函数将格式化的数据写入一个字符串,而`printf()`函数则将格式化的数据输出到标准输出。在Linux环境中,这两个函数被广泛应用于各种编程任务。本文将详细介绍这两个函数的用法,包括格式化字符串的语法和一些常见的使用场景。
297 1
|
11月前
sprintf用法
sprintf用法
108 0
|
11月前
|
编译器 C语言
C语言strlen,strcpy ,strcat, strcmp,strstr常用库函数的理解与模拟实现
C语言strlen,strcpy ,strcat, strcmp,strstr常用库函数的理解与模拟实现
103 0
全网首发:FFMPEG错误: error: implicit declaration of function ‘wcscpy‘/wcscmp; did you mean ‘strcpy‘strcmp?
全网首发:FFMPEG错误: error: implicit declaration of function ‘wcscpy‘/wcscmp; did you mean ‘strcpy‘strcmp?
203 0
strcmp---比较字符串大小的使用要求,实例,自制my_strcmp
strcmp---比较字符串大小的使用要求,实例,自制my_strcmp
81 0
Goland-Println,Printf和 Sprintf 区别
Goland-Println,Printf和 Sprintf 区别
147 0
|
IDE Go 开发工具
Go基础:格式化输出--Printf、Sprintf、Fprintf
Go基础:格式化输出--Printf、Sprintf、Fprintf
455 0