关闭

string的标准库练习

标签: string库源代码strcpy源代码strstr库源代码
119人阅读 评论(0) 收藏 举报
分类:
#include <stdio.h>
#include <assert.h>

size_t strlen(const char *s);
void *memset(void *src, int n, size_t count);
char *strstr(const char *s1, const char *s2);
char *strcat(char *des, const char *src);
char *strncat(char *des, const char *src, size_t n);
int strcmp(const char *s1, const char *s2);
int strncmp(const char *s1, const char *s2, size_t n);
char *mystrcpy(char *des, const char *src);
char *mystrncpy(char *des, const char *src ,size_t n);


size_t strlen(const char *s)//计算字符串长度
{
	if( s == NULL)
		return 0;
	size_t length = 0;
	while(*s)
	{
		length ++;	//长度加一
		s ++;		//地址加一
	}
	return length;
}

void *memset(void *src, int c, size_t count)//第二参数为整形,原因为大多开发人员填入为0,转换成字符就是'\0',也就是终结符,如果是字符参数,不可以要用户填写'\0'吧!!!
{
	assert(src != NULL);//assert是出错提醒,在BUGED有效,在relseas无效,当有两个条件时,无法识别哪个出错
	char *tmp = (char*)src;//取得首地址
	while(count--)
	{
		*tmp = (char)c; 
		tmp ++;
	}
	return tmp;
}

char *strstr(const char *s1, const char *s2)//从字符串找存在指定的小字符串
{
	int i = 0;
	assert(s1 != NULL);
	if (s2 != NULL)
	{
		while( *s1 )
		{
			for(i = 0; *(s1+i) == *(s2+i); i++)
			{
				if( !*(s2+i+1) )
					return (char*)s1;//找到符合条件,字符串1返回找到值以后的全部字符
			}
			s1++;
		}
		return NULL;//搜索完未找到符合的返回NULL;
	}else{
		return (char*)s1;
	}
}

char *strcat(char *des, const char *src) //合并字符串
{
	char *address = des;
	assert( des != NULL );
	assert( src != NULL );
	while(*des)//取得第一个字符串最后一位地址
	{
		des++;	
	}
	while(*des++ = *src++)//将第二个字符串存储区内容赋给第一个字符串后面的
	//以上这句可以理解为 do {*des++ = *src; src++; while( *des != '\0' ) }
	{	
		NULL;
	}
	
	return address;
}

char *strncat(char *des, const char *src, size_t n)
{
	size_t des_len = strlen(des);
	size_t i;
	for (i = 0; i < n && src[i] != '\0'; i++)
	{
		des[des_len + i] = src[i];
	}
	des[des_len + i] = '\0';
	return des;
}

int strcmp(const char *s1, const char *s2)//比较字符串大小
{	
	while( *s1 == *s2 )
	{
		if( *s1 == '\0' )
			return 0;
		s1 ++;
		s2 ++;
	}
	return *s1 - *s2;
}

int strncmp(const char *s1, const char *s2, size_t n)
{	
	while( *s1 == *s2 && n > 0)
	{
		if( *s1 == '\0' )
			return 0;
		s1 ++;
		s2 ++;
		n --;
	}
	return *s1 - *s2;
}

char *mystrcpy(char *des, const char *src)//注意这里可能和系统中strcpy冲突,最好换个函数名称。
{
	assert(des != NULL);
	assert(src != NULL);
	char *addr = des;
	while(*src)
	{
		*des = *src;
		des++;
		src++;
	}
	*des = '\0';
	return addr;
}

char *mystrncpy(char *des, const char *src ,size_t n)//这里和strcpy存在相同问题
{
	assert(des != NULL);
	assert(src != NULL);
	char *addr = des;
	int i = 0;
	for(i = 0; i < n && *src != '\0'; i++)
	{
		*des = *src;
		des++;
		src++;
	}
	*des = '\0';
	return addr;
}



int main()
{
	char x[20]; 
	char y[20]={"abcxyz"};
	char *p_x = y;	
	memset(x, 0, sizeof(x));
	char *x1=strstr(y,"xy");
	printf("%s\n",x1);
	int i = 0;
	for(i = 0; i<5; i++)
		x[i] = 'a' + i;
	printf("x[] = %s\n",x);
	printf("strcat = %s\n",strcat(x, y));
	printf("strncat = %s\n",strncat(x, y,1));
	printf("strcmp = %d\n",strcmp("abd", "abd"));
	printf("strncmp = %d\n",strncmp("abc", "abd", 3));
	printf("x[] = %s\n",x);
	printf("mystrcpy = %s\n",mystrcpy(x,"desdd"));
	printf("mystrncpy = %s\n",mystrncpy(x,"desss",2));
	return 0;
}	

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:14805次
    • 积分:563
    • 等级:
    • 排名:千里之外
    • 原创:40篇
    • 转载:25篇
    • 译文:0篇
    • 评论:0条
    文章分类