strlen,strcmp,strcpy,strcat,strstr,strchr等字符串處理函數的內部實現

1.strlen();

函數介紹(得到字符串的長度):

int strlen(const char *s);

形參:字符串首地址

返回值:字符串長度

內部實現:

int strlen(const char *s) 
{
	int len = 0;
	while(*s++ != '\0')
	{
		len++;
	}
	return len;
}

2.strcmp();

函數介紹(比較兩字符串的大小):

int strcmp(char *s1,char *s2);

形參:兩段字符串的首地址

返回值:若s1>s2,返回一個正值;

                若s1==s2,返回0;

                 若s1<s2,返回一個負值;

內部實現:

int strcmp(char *s1,char *s2)
{
	int ret = 0;
    while (!(ret = *s1 - *s2) && *s2)
    {
        ++s1;
        ++s2;
    }
    if (ret < 0)
    {
        ret = -1;
    }
    else if (ret > 0)
    {
        ret = 1;
    }
    else
    {
        ret = 0;
    }
    return ret;
}

3.strncmp();

函數介紹(比較兩個字符串前n個字符的大小):

int strncmp(const char *s1,const char *s2,int n)

 形參:前面兩個形參是待比較的兩個字符串首地址,第三個形參是想要前n個字符;

返回值:同strcmp()函數,只不過比較的是前n個字符而已;

內部實現:

int strncmp(const char *s1,const char *s2,int n)
{
	int ret = 0;
    while(n && !(ret = *s1 - *s2) && *s2)
    {
        ++s1;
        ++s2;
		--n;
    }
    if (ret < 0)
    {
        ret = -1;
    }
    else if (ret > 0)
    {
        ret = 1;
    }
    else
    {
        ret = 0;
    }
    return ret;
}

4.strcpy();

函數介紹(將一個字符串拷貝給另一個字符串):

char* strcpy(char *dest,const char *src)

 形參:兩字符串的首地址,將src當中的內容拷貝給dest

返回值:拷貝完成後字符串的首地址

內部實現:

char* strcpy(char *dest,const char *src)
{
	if ( dest == NULL || src == NULL)
        return NULL;
    if ( dest == src)
        return dest;
	char *temp = dest;
    while(*src != '\0')
	{
		*dest = *src;
		dest++;
		src++;
	}
    return temp;
}

5.strncpy();

函數介紹(拷貝前n個字符):

char* strncpy(char *dest,const char* src,int n);

 形參:前兩個同上一個函數,第三個參數是想要拷貝字符串的前n個字符

返回值:同上

內部實現:

char* strncpy(char *dest,const char* src,int n)
{
	if ( dest == NULL || src == NULL)
        return NULL;
    if ( dest == src)
        return dest;
	char *temp = dest;
    while(n &&*src != '\0')
	{
		*dest = *src;
		dest++;
		src++;
		n--;
	}
	*dest = '\0';
    return temp;
}

6.strcat();

函數介紹(將一段字符串連接到另一段字符串的後面):

char* strcat(char *dest,const char* src);

形參:兩個字符串的首地址,將src字符串連接到dest後面;

返回值:返回連接後字符串的首地址

內部實現:

char* strcat(char *dest,const char* src)
{
	if ( dest == NULL || src == NULL)
        return NULL;
	char *temp = dest;
	while(*dest != '\0')
	    dest++;
	while(*src != '\0')
	{
		*dest = *src;
		src++;
		dest++;
	}
	
	return temp;
}

7.strncat();

函數介紹(將一段字符串前n個字符連接到另一個字符串上):

char* strncat(char *dest,const char* src,int n);

形參:同上,第三個形參是前n個字符

返回值:同上;

內部實現:

char* strncat(char *dest,const char* src,int n)
{
	if ( dest == NULL || src == NULL)
        return NULL;
	char *temp = dest;
	while(*dest != '\0')
	    dest++;
	while(n && *src != '\0')
	{
		*dest = *src;
		src++;
		dest++;
		n--;
	}
	*dest = '\0';
	return temp;
}

8.strstr();

函數介紹(在s1字符串中查找和s2相同的字符串):

const char *strstr(const char *s1, const char *s2);

形參:兩個字符串的首地址

返回值:若查找到,返回s1中和s2中相同字符串的首地址

                若沒找到,返回NULL

內部實現:

const char *strstr(const char *s1, const char *s2)
{
	const char *temp1;
	const char *temp2 = s2;
	while(*s1 != '\0')
	{ 
        temp1 = s1;
		while(*s1 == *s2)
		{
			s1++;
			s2++;
			if(*s2 == '\0')
				return temp1;
		}
		s1++;
		s2 = temp2;
	}
	return NULL;
}

9.strchr();

函數介紹(在字符串s1中查找和字符ch相同的字符):

const char* strchr(const char *s1,char ch);

形參:一段字符串和要查找的字符

返回值:若找到,返回s1中和ch相等的那個字符的指針

                若沒找到返回NULL

內部實現:

const char* strchr(const char *s1,char ch)
{
	const char *temp;
	while(*s1 != '\0')
	{
		temp = s1;
		if(*s1 == ch)
			return temp;
		s1++;
	}
	return NULL;
}

測試程序:

int main()
{
    char p[] = "jjunku";
	char q[] = "jjunmingyue";
	char m[] = "min";
    // printf("%d\n",strlen(p));
    // printf("%d\n",strncmp(p,q,5));
	// printf("%d\n",strncmp(p,q,6));
	// printf("%s\n",strcpy(q,p));  
	// printf("%s\n",strncpy(q,p,2));
	// printf("%s\n",strcat(q,p));
	// printf("%s\n",strncat(q,p,2));
	printf("%s\n",strstr(q,m));
	printf("%s\n",strchr(p,'u'));
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章