C语言实现字符串的复制,比较,模式匹配,替换等各种操作。

c语言中没有字符串,想要用字符串只能用字符指针来实现了。这些函数都是自己学习C后复现的,和C的模板库中的会有差别。字符串的常见操作有:

strcpy(p, p1)将字符串p1复制到p的开头

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

strncpy(p, p1, n) 复制指定长度字符串p不能比p1短

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

strlen(p) 取字符串长度,不包括结束符‘\0’

int strlen(char* dest) {
	if (dest == NULL) return 0;
	int length = 0;
	while (*(dest++) != '\0')
		++length;
	return length;
}

strcat(p, p1) 附加字符串将p1加到p的尾部

char* strcat(char* dest, char* src) {
	if (dest == NULL || src == NULL) return NULL;
	int srclen = strlen(src);
	int destlen = strlen(dest);
	char* ret = (char*)malloc((destlen + srclen + 1) * sizeof(char));
	char* tmp = ret;
	while (*dest != '\0') {
		*ret++= *dest++;
	}
	while (*src != '\0') {
		*ret++ = *src++;
	}
	*ret = '\0';
	return tmp;
}

strncat(p, p1, n) 附加指定长度字符串,将p1的前n个字符复制到p的尾部

char* strncat(char* dest, char* src, int n) {
	if (dest == NULL || src == NULL || n <= 0) {
		return NULL;
	}
	char* tmp = (char*)malloc((strlen(dest) + n + 1) * sizeof(char));
	char* ret = tmp;
	while (*dest != '\0') {
		*tmp++ = *dest++;
	}
	int i = 0;
	while (*src != '\0' && i

strcmp(p, p1) 比较字符串

int strcmp(char* dest, char* src) {
	if (dest == NULL && src == NULL) return 0;
	int i = 0;
	while ((*dest != '\0') && (*src != '\0')) {
		if (*dest > *src) return 1;
		else if (*dest < *src) return -1;
		else {
			++dest;
			++src;
		}
	}
	if (*dest == '\0' && *src=='\0') return 0;
	else if (*dest == '\0') return -1;
	else return 1;
}

strcasecmp(p,p1)忽略大小写比较字符串

//辅助函数,将大字母转换成小写
char tolower(const char c) {
	return (('A' <= c) && (c <= 'Z')) ? (c - 'A' + 'a') : c;
}
int strcasecmp(char* dest, char *src) {
	char* p1 = dest;
	char* p2 = src;
	int result = 0;
	if (p1 == p2) {
		return 0;
	}
	while ((result =tolower(*p1) - tolower(*p2)) == 0) {
		if (*(++p1) == '\0') break;
		++p2;
	}
	return result;

}

strncmp(p, p1, n) 比较指定长度字符串

int strncmp(const char* dest, const char* src, int n) {
	const unsigned char* p1 = (const unsigned char*)dest;
	const unsigned char* p2 = (const unsigned char*)src;
	if (p1 == p2) return 0;
	int result=0,i=0;
	while ((i < n) && ((result = *p1 - *p2) == 0)) {
		if (*(++p1) == '\0') break;
		++p2;
		++i;
	}
	return result;
}

strchr(p, c) 在字符串中查找指定字符

int strchr1(const char* dest, const char c) {
	const unsigned char* p = (const unsigned char*)dest;
	int i = 1;
	while (*p != '\0') {
		if (*p++ == c) return i;
		++i;
	}
	return 0;
}

strstr(p, p1) 查找字符串,返回匹配的位置

int strstr1(const char* dest, const char* src) {
	const unsigned char* p = (const unsigned char*)dest;
	int i = 1;
	while (*p != '\0') {
		const unsigned char* tmp = p;
		const unsigned char* p1 = (const unsigned char*)src;
		while (*tmp== *p1) {
			if (*(++p1)== '\0') break;
			++tmp;
		}
		if (*p1 == '\0') return i;
		++p;
		++i;
	}
	return 0;
}
strreplace(p,p1,p2,p3)字符串替换,将p中的p2全部替换成p3
//先查找,返回匹配处的指针
char* strstr2(const char* dest, const char* src) {
	const unsigned char* p = (const unsigned char*)dest;
	while (*p != '\0') {
		const unsigned char* tmp = p;
		const unsigned char* p1 = (const unsigned char*)src;
		while (*tmp == *p1) {
			if (*(++p1) == '\0') break;
			++tmp;
		}
		if (*p1 == '\0') return (char*)p;
		++p;
	}
	return NULL;
}
char* strreplace(const char* dest, const char* src, const char* rep) {
    char* p = (char*)dest;
	char* tmp = strstr2(dest, src);
	while (tmp != NULL) {
		char* secstr = tmp + strlen(src);     //后部分的地址
		*tmp = '\0';                         //截断,获取前部分
		p=strcat((char*)p, (char*)rep); //将要替换的连接到前的尾部
		//printf("%s\n", p);
		p = strcat(p, secstr);            //再将后部分连接起来,构成新的串
		tmp = strstr2(p, src);            //再次查找
	}
	return p;
}

函数验证

#include "stdafx.h"
#include
#include
using namespace std;

//各种函数
int main()
{
	//string
	char dest[] = "abcdefbcdncsanibcd";
	char src[] = "bcd";
	char rep[] = "BCD";
	char d = 'c';
	//strcpy_s(dest, src);库函数
	/*strncpy(dest, src, 15);*/
	//printf("%d", strstr1(dest,src));
	printf("%s", strreplace(dest,src,rep));
	printf("\n");
    return 0;
}




















你可能感兴趣的:(C,c++学习笔记)