zoukankan      html  css  js  c++  java
  • 面试中常考的C函数库中的字符串处理函数

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<assert.h>
    
    
    int myStrlen(const char *strDest)
    {
    	assert(NULL != strDest);
    	const char *p = strDest;
    	int len = 0;
    	while( (*p) != '\0' )
    	{
    		len++;
    		p++;
    	}
    	return len;
    }
    
    int itrStrlen(const char *strDest)
    {
    	assert(NULL != strDest);
    	return ('\0' != *strDest) ? (1 + itrStrlen(strDest+1)) : 0;
    }
    
    char* myStrcpy(char *strDst, const char *strSrc)
    {
    	assert(NULL != strDst && NULL != strSrc);
    	char *strRst = strDst;
    	while( *strSrc != '\0' )
    		*(strDst++) = *(strSrc++);
    	return strRst;
    }
    
    int myStrcmp(const char *strL, const char *strR)
    {
    	assert(NULL != strL && NULL != strR);
    	int ret = 0;
    	const char *left = strL;
    	const char *right = strR;
    	while( (!(ret = (*left - *right))) && (*right != '\0') )
    	{
    		left++;
    		right++;
    	}
    	if(ret < 0) ret = -1;
    	else if(ret > 0) ret = 1;
    	return ret;
    }
    //???上网查
    void * myMemset(void *buffer, int c, size_t count)
    {
    	assert(NULL != buffer);
    	char *p = (char *)buffer;
    	while(count--)
    	{
    		*p = 'c'; 
    		printf("%d", *p);
    		p++;
    	}
    	return buffer;
    }
    
    char * myStrchr(char *str, int c)
    {
    	assert(NULL != str);
    	for(; *str != (char)c; str++);
    	if(*str == '\0') return NULL;
    	return str;
    }
    
    char * myStrcat(char *strDes, const char *strSrc)
    {
    	assert(NULL != strDes && NULL != strSrc);
    	char *addr = strDes;
    	while(*strDes != '\0') strDes++;
    	while( (*strDes++ = *strSrc++) != '\0' );
    	return addr;
    }
    //*有点难度
    void * myMemcpy(void *to, const void *from, size_t count)
    {
    	assert(NULL != to && NULL != from);
    	void *ret = to;
    	char *pto = (char *)to;
    	char *pfrom = (char *)from;
    	assert(pto < pfrom || pto > (pfrom+count-1));//!!!
    	while(count--)
    	{
    		*pto++ = *pfrom++;
    	}
    	return ret;
    }
    
    void test()
    {
    	//test strlen
    	char *s = "dfdafdaf";
    	int len = myStrlen(s);
    	char *ss = "";
    	len = myStrlen(ss);
    	printf("%d\n", len);
    
    	//test strcpy
    	char *str = (char *)malloc(sizeof(char)*10);
    	strcpy(str, s);
    	//strcpy(str, "abcdefg");
    	//strcpy(ss, s); //由于未对ss分配内存,因此会出现异常
    	printf("%s\n", str);
    
    	//test strcmp
    	int ret = myStrcmp(str, s);
    	printf("%d\n", ret);
    	ret = myStrcmp(str, ss);
    	printf("%d\n", ret);
    	ret = myStrcmp(str, "zfdfd");
    	printf("%d\n", ret);
    
    	//test memset
    	int a[10];
    	myMemset(a, 1, sizeof(int)*10);
    
    	//test strchr
    	char *p = myStrchr(s, 'a');
    	printf("\n%c\n", *p);
    
    	//test strcat
    	//char *cat = (char *)malloc(sizeof(char)*20);
    	myStrcat(str, "aab");
    	printf("%s\n", str);
    
    
    }
    
    
    int main()
    {
    	test();
    	return 0;
    }


  • 相关阅读:
    Twitter如何在数千台服务器上快速部署代码?
    系统架构师学习笔记_第六章(上)_连载
    使用IIS内置压缩功能,增加网站访问速度
    系统架构师学习笔记_第八章_连载
    微软企业库4.1学习笔记(十五)缓存模块3 使用数据库作为后端存储
    快速搞懂 SQL Server 的锁定和阻塞
    微软企业库4.1学习笔记(十四)缓存模块2 使用缓存模块进行开发
    微软企业库4.1学习笔记(十六)缓存模块4 服务器场中的缓存使用
    Agile PLM Engineering Collaboration
    EC Client Customizing EC Client 客户化
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3022812.html
Copyright © 2011-2022 走看看