//mystrcpy
#include <stdio.h>
char *mystrcpy(char *dest, char *src)
{
int i;
for(i = 0; (dest[i] = src[i]); i++){
; //当src[i] = "\0" 表达式为0,0为假,条件不成立,跳出循环
}
return dest;
}
int main(int argc, char *argv[])
{
char dest[200];
printf("mystrcpy:%s\n", mystrcpy(dest, argv[1]));
return 0;
}
/*
akaedu@akaedu-G41MT-D3:~/lin/722_str$ ./mystrcpy hellook
mystrcpy:hellook
*/<pre name="code" class="cpp">
//*******************************************
//--------------------mystrcpy1---------------
#include <stdio.h>
#include <string.h>
char *mystrcpy(char *dest, char *src)
{
int i = 0;
for(; (dest[i] = src[i]); i++){
;
}
return dest;
}
int main(void)
{
char dest[100];
char src[100];
gets(src);
// fgets(src, 100, stdin);
printf("%s\n", mystrcpy(dest, src) );
return 0;
}
/*如果用fgets()会把回车也读进去//去回车输出
int i = strlen( mystrcpy(dest, src) );
printf("%d\n", i);
if(dest[i-1] == '\n')
{
dest[i-1] = '\0';
}
printf("%s\n", dest);
return 0;
}
--------------------------------------
akaedu@akaedu-G41MT-D3:~/lin/722_str$ ./mystrcpy1
hellow
7
hellow
--------------------------------------
*/<pre name="code" class="cpp">
//mystrncpy
#include <stdio.h>
char *mystrncpy(char *dest, char *src, int n )
{
int i;
for(i = 0; ( (n > 0) && (dest[i] = src[i]) ); i++, n-- ){
}
while(n >= 0){
dest[i++] = '\0';
n--;
}
//dest[i++] = '\0';
return dest;
}
int main(int argc, char *argv[])
{
char str[200];
printf("mystrncpy:%s\n", mystrncpy(str, argv[1], 5));
return 0;
}
/*
akaedu@akaedu-G41MT-D3:~/lin/722_str$ ./mystrncpy helloaaa
mystrncpy:hello
akaedu@akaedu-G41MT-D3:~/lin/722_str$ ./mystrncpy comeonbaby
mystrncpy:comeo
*/
//*******************************************
//--------------------mystrncpy1---------------
#include <stdio.h>
#include <string.h>
char *mystrncpy(char *dest, char *src, int n)
{
int i;
// for( i = 0; ( (i > n) &&(dest[i] = src[i]) ); i++){
// ;
// }
for( i = 0; ( (n > 0) &&(dest[i] = src[i]) ); i++, n--){
;
}
while( n >= 0){
dest[i++] = '\0';
n--;
}
return dest;
}
int main(void)
{
char dest[100];
char src[100];
gets(src);
// fgets(src, 100, stdin);
printf("%s\n", mystrncpy(dest, src, 5) );
return 0;
}
/*
akaedu@akaedu-G41MT-D3:~/lin/722_str$ ./mystrncpy1
hellowor ds
hello
*/
<pre name="code" class="cpp">
/***************************************************
自己实现库函数:strtok
*/
#include <stdio.h>
#include <string.h>
char *mystrtok(char *src, char *delim)
{
char *begin, *end;
static char *savep = "";
begin = src ? src : savep;
begin = begin + strspn(begin, delim);
if(*begin == '\0'){
savep = "";
return NULL;
}
end = begin + strcspn(begin, delim);
if(*end != '\0'){
*end++ = '\0';
}
savep = end;
return begin;
}
int main(int argc, char *argv[])
{
char *token, *begin;
for(begin = argv[1]; (token = mystrtok(begin, argv[2])); begin = NULL ){
printf("token:%s\n", token);
}
return 0;
}
//************************************
//-----------mystrlen-----------------
#include <stdio.h>
int mystrlen(char *src)
{
int i = 0;
for(i = 0; (src[i] != '\0') ; i++){
printf("src[%d]:%d %c\n", i, i, src[i]);
}
return i;
}
int main(void)
{
char str[1024];
fgets(str, 1024, stdin);
// gets(str);
printf("%d\n", mystrlen(str));
return 0;
}
/*----------------------
hello
src[0]:0 h
src[1]:1 e
src[2]:2 l
src[3]:3 l
src[4]:4 o
src[5]:5
5
-----------------------*/
<pre id="best-content-1042162848" class="best-text mb-10">fgets的原型是
char* fgets(char* s, int n, FILE* fp);
参数数量比较多,有3个。而fgets相比于gets有一个显著的差别就是fgets会将行末的换行符算到读入的字符串里面。所以相同且正常(输入无错误,缓冲区够大)的情况下,fgets读入的字符串会比gets在末尾'\0'前面多一个换行符;行长度超出缓冲区大小时只读入前 n-1 个字符。因此,
gets(s);
相当于
fgets(s, sizeof(s), stdin);
if(s[strlen(s) - 1] == '\n') s[strlen(s) - 1] = '\0'; // 去掉换行符