字符串函数自定义

       字符串函数(String processing function)也成字符串处理函数,指的是编程语言中用来进行字符串处理的函数,如C,pascal,Visual以及LotusScript中进行字符串拷贝,计算长度,字符查找等的函数。

     strcpy

功能:把src所指由NUL结束的字符串复制到dest所指的数组中。

strncpy

功能:把src所指由NULL结束的字符串的前n个字节复制到dest所指的数组中。
strcat
功能:把src所指字符串添加到dest结尾处(覆盖dest结尾处的'\0')并添加'\0'。

strncat

功能:把src所指字符串的前n个字符添加到dest结尾处(覆盖dest结尾处的'\0')并添加'\0'。

strcmp

功 能: 将一个串与另一个比较

strncmp

功 能: 把串中的一部分与另一串中的一部分比较 (前n个字符)

strstr

功 能: 在串中查找指定字符串的第一次出现

strchr

功 能: 在串中查找指定字符的第一次出现

strlen

功 能: 求字符串的长度
下面是代码实现
“str.h”
<strong><span style="font-size:18px;">#pragma once
#include <assert.h>

char* MySrtcpy(char* dest,const char* src)
{
	assert(dest);
	assert(src);
	char* ret = dest;

	while (*src != '\0')
	{
		*dest++ = *src++;
		//dest++;
		//src++;
	}
	*dest = '\0';

	return ret;
}


char* MyStrncpy(char* dest,const char* src,int count)
{
	assert(dest);
	assert(src);
	char* ret = dest;

	while (count && (*src != '\0'))
	{
		*dest++ = *src++;
		count--;
	}
	*dest = '\0';

	return ret;
}

char* MyStrcat(char* dest,const char* src)
{
	assert(dest);
	assert(src);
	char* ret = dest;

	while (*dest != '\0')
	{
		dest++;
	}

	while (*src != '\0')
	{
		*dest++ = *src++;
	}
	*dest = '\0';

	return ret;
}

char* MySrtncat(char* dest,const char* src,int count)
{
	assert(dest);
	assert(src);
	char* ret = dest;

	while (*dest != '\0')
	{
		dest++;
	}

	while ((*src != '\0') && count)
	{
		*dest++ = *src++;
		count--;
	}
	*dest = '\0';

	return ret;
}

int MyStrcmp(const char* dest,const char* src)
{
	assert(dest);
	assert(src);

	while (*dest == *src)
	{
		dest++;
		src++;
	}

	int ret = (((unsigned int)*dest) - ((unsigned int)*src));

	if (ret > 0)
	{
		ret = 1;
	}
	else if (ret < 0)
	{
		ret = -1;
	} 
	else
	{
		ret = 0;
	}

	return ret;
}

int MyStrncmp(const char* dest,const char* src,int count)
{
	assert(dest);
	assert(src);

	while ((*dest == *src) && (count))
	{
		count--;
		if (count == 0)
		{
			//若是n都比较完了,那么指针就不要向后偏移了
			break;
		}
		dest++;
		src++;
	}

	return (((unsigned int)*dest)-((unsigned int)*src));
}

char* MyStrstr(const char* str1,const char* str2)
{
	assert(str1);
	assert(str2);

	const char* new_str1 = NULL;
	const char* new_str2 = NULL;

	while (*str1 != '\0')
	{
		new_str1 = str1;
		new_str2 = str2;

		while ((*new_str1) && (*new_str2) && (*new_str1 == *new_str2))
		{
			new_str1++;
			new_str2++;
		}

		if (*new_str2 == '\0')
		{
			return (char* )str1;
		}

		str1++;
	}

	return NULL;
}

char* MyStrchr(const char* str,const char c)
{
	assert(str);

	while (*str != '\0')
	{
		if (*str == c)
		{
			return (char *)str;
		}
		str++;
	}

	return NULL;
}

int MyStrlen(const char* str)
{
	assert(str);
	int count = 0;

	while (*str != '\0')
	{
		count++;
		str++;
	}
	return count;
}

int MyStrlen_OP(const char* str)
{
	assert(str);
	const char* str_end = str;
	const char* str_start = str;

	while (*str_end != '\0')
	{
		str_end++;
	}

	return (str_end - str_start);
}</span></strong>


"test.cpp"
<strong><span style="font-size:18px;">#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;
#include "StrMen.h"

void Test1()
{
	char arr[20] = "abcdefgh";
	char *p = "hello bit";
	//strcpy(arr,p);
	MySrtcpy(arr,p);
	cout<<arr<<endl;
}

void Test2()
{
	char arr[20] = "hello_whord";
	char *p = "strncpy";
	//strncpy(arr,p,8);
	MyStrncpy(arr,p,5);
	cout<<arr<<endl;
}

void Test3()
{
	char arr[20] = "hello ";
	char *p = "world";
	//strcat(arr,p);
	MyStrcat(arr,p);
	cout<<arr<<endl;
}

void Test4()
{
	char arr[20] = "hello ";
	char *p = "world";
	//strncat(arr,p,3);
	MySrtncat(arr,p,7);
	cout<<arr<<endl;
}

void Test5()
{
	char *p = "abcded";
	char *q = "abcdea";
	//int ret = strcmp(p,q);
	int ret = MyStrcmp(p,q); 
	cout<<ret<<endl;
}

void Test6()
{
	char *p = "abcde";
	char *q = "abciab";
	//int ret = strncmp(p,q,3);//
	int ret = MyStrncmp(p,q,5);
	cout<<ret<<endl;
}

void Test7()
{
	char *p = "hello world";
	char *q = "ello";
	//cout<<strstr(p,q)<<endl;
	cout<<MyStrstr(p,q)<<endl;
}

void Test8()
{
	char *p = "abcde";
	char c = 'd';
	//cout<<strchr(p,c)<<endl;
	cout<<MyStrchr(p,c)<<endl;
}

void Test9()
{
	char *p = "hello";
	char arr[] = "bit";
	//cout<<strlen(p)<<endl;
	//cout<<strlen(arr)<<endl;
	cout<<MyStrlen(p)<<endl;
	cout<<MyStrlen(arr)<<endl;

	cout<<MyStrlen_OP(p)<<endl;
	cout<<MyStrlen_OP(arr)<<endl;
}

int main()
{
	//Test1();//strcpy
	//Test2();//strncpy
	//Test3();//strcat
	//Test4();//strncat
	//Test5();//strcmp
	//Test6();//strncmp
	//Test7();//strstr
	//Test8();//strchr
	Test9();//strlen
	system("pause");
	return 0;
}</span></strong>


你可能感兴趣的:(C++,字符串,函数)