C语言-字符函数和字符串函数

0. 前言

C语言中对字符和字符串的处理很是频繁,但是C语言本身是没有字符串类型的,字符串通常放在常量字符串中或者 字符数组中。

字符串常量适用于那些对它不做修改的字符串函数.

1、函数介绍

1.1 求字符串长度

strlen

size_t strlen ( const char * str );
  • 字符串已经 ‘\0’ 作为结束标志,strlen函数返回的是在字符串中'\0' 前面出现的字符个数(不包含 '\0' )
  • 参数指向的字符串必须要以'\0'结束
  • 注意函数的返回值为size_t,是无符号的( 易错

代码实现:

#include <stdio.h>
#include <string.h>

int main()
{
	const char*str1 = "abcdef";
	const char*str2 = "bbb";
	if(strlen(str2)-strlen(str1)>0)
	{
		printf("str2>str1\n");
	}
	else
	{
		printf("srt1>str2\n");
	}
	return 0;
}

模拟实现strlen

#include <stdio.h>
#include <string.h>
#include <assert.h>

size_t my_strlen(const char* str)
{
	size_t count = 0;
	assert(str);
	while (*str != '\0')
	{
		count++;
		str++;
	}
	return count;
}
int main()
{
	char arr[] = "abcdef";
	size_t n = my_strlen(arr);
	printf("%u\n", n);//6
    
	return 0;
}

1.2 长度不受限制的字符串函数

strcpy

char* strcpy(char * destination, const char * source );
  • Copies the C string pointed by source into the array pointed by destination, including the terminating null character (and stopping at that point).
  • 源字符串必须以 '\0'结束
  • 会将源字符串中的 ‘\0’ 拷贝到目标空间
  • 目标空间必须足够大,以确保能存放源字符串
  • 目标空间必须可变

代码实现:

#include <stdio.h>
#include <string.h>

int main()
{
	char name[10] = "";
	char arr[] = "abcdef";

	strcpy(name, arr);
	printf("%s\n", name);
    
	return 0;
}

模拟实现strcpy

#include <stdio.h>
#include <string.h>
#include <assert.h>

char* my_strcpy(char* dest, const char* src)
{
	assert(dest && src);
	char* ret = dest;
	while (*dest++ = *src++)
		;
	return ret;
}
int main()
{
	char arr1[20] = "";
	char arr2[] = "abcdef";
	char* ret = my_strcpy(arr1, arr2);
	printf("%s\n", ret);
    
	return 0;
}

strcat

char * strcat ( char * destination, const char * source );
  • Appends a copy of the source string to the destination string. The terminating null character in destination is overwritten by the first character of source, and a null-character is included at the end of the new string formed by the concatenation of both in destination.
  • 源字符串必须以 ‘\0’ 结束
  • 目标空间必须有足够的大,能容纳下源字符串的内容
  • 目标空间必须可修改

代码实现:

#include <stdio.h>
#include <string.h>

int main()
{
	char arr1[20] = "hello ";
	char arr2[] = "world";
	strcat(arr1, arr2);
	printf("%s\n", arr1);

    return 0;
}

模拟实现strcat

#include <stdio.h>
#include <string.h>
#include <assert.h>

char* my_strcat(char* dest, const char* src)
{
	char* ret = dest;
	assert(dest && src);
	//1. 找到目标空间的末尾\0
	while (*dest != '\0')
	{
		dest++;
	}
	//2. 拷贝字符串
	while (*dest++ = *src++)
	{
		;
	}
	return ret;
}
int main()
{
	char arr1[20] = "hello ";
	char arr2[] = "world";
	char* ret = my_strcat(arr1, arr2);
	printf("%s\n", ret);
    
	return 0;
}

strcmp

int strcmp ( const char * str1, const char * str2 );
  • This function starts comparing the first character of each string. If they are equal to each other, it continues with the following pairs until the characters differ or until a terminating null-character is reached.
  • 标准规定:
    1. 第一个字符串大于第二个字符串,则返回大于0的数字
    2. 第一个字符串等于第二个字符串,则返回0
    3. 第一个字符串小于第二个字符串,则返回小于0的数字

模拟实现

#include <stdio.h>
#include <string.h>
#include <assert.h>

int my_strcmp(const char* str1, const char* str2)
{
	assert(str1 && str2);
	while (*str1 == *str2)
	{
		if (*str1 == '\0')
			return 0;//相等
		str1++;
		str2++;
	}
	return (*str1 - *str2);
}
int main()
{
	char arr1[20] = "zhangsan";
	char arr2[] = "zhangsanfeng";

	//两个字符串比较相等,应该使用strcmp
	int ret = my_strcmp(arr1, arr2);
	if (ret < 0)
		printf("<\n");
	else if (ret == 0)
		printf("==\n");
	else
		printf(">\n");
    
	return 0;
}

1.3 长度受限制的字符串函数介绍

strncpy

char * strncpy ( char * destination, const char * source, size_t num );
  • Copies the first num characters of source to destination. If the end of the source C string (which is signaled by a null-character) is found before num characters have been copied, destination is padded with zeros until a total of num characters have been written to it.
  • 拷贝num个字符从源字符串到目标空间
  • 如果源字符串的长度小于num,则拷贝完源字符串之后,在目标的后边追加0,直到num个

代码实现:

#include <stdio.h>
#include <string.h>

int main()
{
	char arr1[20] = "abcdef";
	char arr2[] = "bit";
	//strcpy(arr1, arr2);
	strncpy(arr1, arr2, 5);
	printf("%s\n", arr1);
    
	return 0;
}

strncat

char * strncat ( char * destination, const char * source, size_t num );
  • Appends the first num characters of source to destination, plus a terminating null-character.
  • If the length of the C string in source is less than num, only the content up to the terminating null-character is copied.

代码实现:

#include <stdio.h>
#include <string.h>

int main()
{
	char arr1[20] = "hello\0xxxxxx";
	char arr2[] = "bit";
	strncat(arr1, arr2, 6);
	printf("%s\n", arr1);
	return 0;
}

strncmp

int strncmp ( const char * str1, const char * str2, size_t num );
  • 比较到出现另个字符不一样或者一个字符串结束或者num个字符全部比较完。

代码实现:

#include <stdio.h>
#include <string.h>

int main()
{
	char arr1[] = "abcdef";
	char arr2[] = "abcq";
	int ret = strncmp(arr1, arr2, 4);
	printf("%d\n", ret);

	if (ret == 0)
		printf("==\n");
	else if(ret<0)
		printf("<\n");
	else 
		printf(">\n");
    
	return 0;
}

1.4 字符串查找

strstr

char * strstr ( const char *str1, const char * str2);
  • Returns a pointer to the first occurrence of str2 in str1, or a null pointer if str2 is not part of
    str1.
#include <stdio.h>
#include <string.h>

int main()
{
	char email[] = "bye121345@gitee.com";
	char substr[] = "gitee";

	char* ret = strstr(email, substr);
	if (ret == NULL)
	{
		printf("子串不存在\n");
	}
	else
	{
		printf("%s\n", ret);
	}
	return 0;
}

模拟实现strstr

#include <stdio.h>
#include <string.h>
#include <assert.h>

char* my_strstr(const char* str1, const char* str2)
{
	assert(str1 && str2);
	const char* s1 = str1;
	const char* s2 = str2;
	const char* p = str1;

	while (*p)
	{
		s1 = p;
		s2 = str2;
		while (*s1 != '\0' && *s2 != '\0' && *s1 == *s2)
		{
			s1++;
			s2++;
		}
		if (*s2 == '\0')
		{
			return (char*)p;
		}
		p++;
	}
	return NULL;
}
int main()
{
	char email[] = "bye12345@gitee.com";
	char substr[] = "gitee";

	char* ret = my_strstr(email, substr);
	if (ret == NULL)
	{
		printf("子串不存在\n");
	}
	else
	{
		printf("%s\n", ret);
	}
	return 0;
}

strtok

char * strtok ( char * str, const char * sep );
  • sep参数是个字符串,定义了用作分隔符的字符集合
  • 第一个参数指定一个字符串,它包含了0个或者多个由sep字符串中一个或者多个分隔符分割的标记
  • strtok函数找到str中的下一个标记,并将其用 \0 结尾,返回一个指向这个标记的指针。(注:strtok函数会改变被操作的字符串,所以在使用strtok函数切分的字符串一般都是临时拷贝的内容并且可修改。)
  • strtok函数的第一个参数不为 NULL ,函数将找到str中第一个标记,strtok函数将保存它在字符串中的位置
  • strtok函数的第一个参数为 NULL ,函数将在同一个字符串中被保存的位置开始,查找下一个标记
  • 如果字符串中不存在更多的标记,则返回 NULL 指针。

代码实现:

#include <stdio.h>
#include <string.h>

int main()
{
	const char* sep = "@.";
	char email[] = "bye12345@gitee.com.net";
	char cp[40] = { 0 };
	strcpy(cp, email);

	char* ret = NULL;
	for (ret = strtok(cp, sep);
		ret != NULL;
		ret = strtok(NULL, sep))
	{
		printf("%s\n", ret);
	}
	return 0;
}

1.5 错误信息报告

strerror

char * strerror ( int errnum );
  • 返回错误码,所对应的错误信息。

代码实现:

#include <stdio.h>
#include <string.h>
#include <errno.h>
//C语言的库函数,在执行失败的时候,都会设置错误码
int main()
{
	printf("%s\n", strerror(0));
	printf("%s\n", strerror(1));
	printf("%s\n", strerror(2));
	printf("%s\n", strerror(3));
	printf("%s\n", strerror(4));
	printf("%s\n", strerror(5));

	//errno - C语言设置的一个全局的错误码存放的变量
	
	FILE* pf = fopen("C:\\c-language\\c\\txt.txt", "r");
	if (pf == NULL)
	{
		printf("%s\n", strerror(errno));
		return 1;
	}
	else
	{
		//
	}
	return 0;
}

字符分类函数

函数如果他的参数符合下列条件就返回真
iscntrl任何控制字符
isspace空白字符:空格‘ ’,换页‘\f’,换行’\n’,回车‘\r’,制表符’\t’或者垂直制表符’\v’
isdigit十进制数字 0~9
isxdigit十六进制数字,包括所有十进制数字,小写字母af,大写字母AF
islower小写字母a~z
isupper大写字母A~Z
isalpha字母az或AZ
isalnum字母或者数字,az,AZ,0~9
ispunct标点符号,任何不属于数字或者字母的图形字符(可打印)
isgraph任何图形字符
isprint任何可打印字符,包括图形字符和空白字符
#include <stdio.h>
#include <ctype.h>

int main()
{
   int a = isspace(' ');
   printf("%d\n", a);

   int b = isdigit('x');
   printf("%d\n", b);

   printf("%c\n", tolower('@'));
   return 0;
}

1.6 内存操作函数

memcpy

void * memcpy ( void * destination, const void * source, size_t num );
  • 函数memcpy从source的位置开始向后复制num个字节的数据到destination的内存位置
  • 这个函数在遇到 ‘\0’ 的时候并不会停下来
  • 如果source和destination有任何的重叠,复制的结果都是未定义的。

代码实现:

#include <stdio.h>
#include <string.h>

int main()
{
	float arr3[5] = { 1.0,2.5,3.0,5.0,6.0 };
	float arr4[10] = { 0.0 };
	memcpy(arr4, arr3, 20);

	return 0;
}

模拟实现memcpy

#include <stdio.h>
#include <string.h>
#include <assert.h>

void* my_memcpy(void* dest, const void* src, size_t num)
{
	assert(dest && src);
	void* ret = dest;

	while (num--)
	{
		*(char*)dest = *(char*)src;
		dest = (char*)dest + 1;
		src = (char*)src + 1;
	}

}
int main()
{
	int arr1[] = { 1,2,3,4,5,6,7,8,9,10 };
	int arr2[10] = { 0 };
	my_memcpy(arr2, arr1, 28);

	return 0;
}

memmove

void * memmove ( void * destination, const void * source, size_t num );
  • 和memcpy的差别就是memmove函数处理的源内存块和目标内存块是可以重叠的
  • 如果源空间和目标空间出现重叠,就得使用memmove函数处理。

memcmp

int memcmp ( const void * ptr1,
			const void * ptr2,
			size_t num );
  • 比较从ptr1和ptr2指针开始的num个字节
  • 返回值如下

Returns an integral value indicating the relationship between the content of the memory blocks:

(返回一个整数值,表示内存块内容之间的关系):

返回值(return value)indicates
<0the first byte that does not match in both memory blocks has a lower value in ptr1 than in ptr2 (if evaluated as unsigned char values)
0the contents of both memory blocks are equal
<0the first byte that does not match in both memory blocks has a greater value in ptr1 than in ptr2 (if evaluated as unsigned char values)
#include <stdio.h>
#include <string.h>

int main()
{
	int arr1[] = { 1,2,3,4,5 };
	int arr2[] = { 1,3,2 };
	int ret = memcmp(arr1, arr2, 12);
	printf("%d\n", ret);

	return 0;
}

本博客代码仅供参考,如有错误,欢迎指正

  • 32
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 32
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 32
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Bowen…

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值