【C语言】字符函数和字符串函数小结(1)

此处是字符函数和字符串函数一个学习小结,有问题的还需大家指正。

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

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

  • 求字符串长度函数

strlen

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

strcpy

strcat

strcmp

  • 长度受限制的字符串函数

strncpy

strncat

strncmp

  • 字符串查找函数

strchr

strrchr

strpbrk

strstr

  • 高级字符串查找函数

strspn

strcspn

strtok

  • 错误信息报告函数

strerror

  • 字符操作函数
  • 内存操作函数

memcpy

memmove

memset

memcmp

memchr

函数介绍

求字符串长度函数

  • strlen

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

实例:

#define _CRT_SECURE_NO_WARNINGS 1

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

int main()
{
	if (strlen("abcdef") - strlen("abcdefghi") > 0)
	{
		printf("hehe\n");
	}
	else
	{
		printf("haha\n");
	}
	system("pause");
	return 0;
}

显然输出的结果是hehe,这就是因为strlen函数的返回值是size_t,是无符号的.两个无符号数相减还是无符号数,这里把-3的补码当做无符号数来处理,是一个很大的整数,因此输出的是hehe.

  • 模拟strlen函数的实现:

非递归实现:

#define _CRT_SECURE_NO_WARNINGS 1

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

int _strlen(const char *str)
{
	assert(str != NULL);
	int count = 0;
	while (*str++)
	{
		count++;
	}
	return count;
}

int main()
{
	char *str = "abcdef";
	printf("%d\n", _strlen(str));
	system("pause");
	return 0;
}

递归实现:

#define _CRT_SECURE_NO_WARNINGS 1

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

int _strlen(const char *str)
{
	assert(str);
	if (*str != '\0')
	{
		return 1 + _strlen(str + 1);
	}
	else
	{
		return 0;
	}
}

int main()
{
	char *str = "abcdef";
	printf("%d\n", _strlen(str));
	system("pause");
	return 0;
}

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

  • strcpy

将源指向的C字符串复制到目标指向的数组中,包括终止字符(并在该点停止)

char *strcpy(char *destination,const char *source);
  1. 目标空间必须是可以被修改
  2. 目标空间的空间必须足够大,以确保能存放源字符串
  3. 源字符串必须以'\0'结束,源数据中必须有'\0',才能停下来
  4. 会将源字符串中的'\0'拷贝到目标空间
  • 模拟实现strcpy
#define _CRT_SECURE_NO_WARNINGS 1

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

char* _strcpy(char *dest, const char *src)
{
	assert(dest&&src);
	char *ret = dest;
	while (*dest++ = *src++)
	{
		;
	}
	return ret;
}

int main()
{
	char arr[10] = { 0 };
	char *p = "abcdef";
	printf("%s\n", _strcpy(arr, p));
	system("pause");
	return 0;
}
  • strcat

将源字符串的副本附加到目标字符串。在目标字符串中的终止字符由源字符串的第一个字符覆盖,并且在由目标字符串中这两个字符连接形成的新字符串的末尾包含一个终止字符。

char* strcat(char *destination,const char *source);
  1. 两个字符串必须以'\0'结束
  2. 目标空间的空间必须足够大,以确保能容纳下源字符串
  3. 目标空间必须可修改
  4. 自己无法给自己追加
  • 模拟实现strcat
#define _CRT_SECURE_NO_WARNINGS 1

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

char* _strcat(char *dest, const char *src)
{
	assert(dest&&src);
	char *ret = dest;
	while (*dest)
	{
		dest++;
	}
	while (*dest++ = *src++)
	{
		;
	}
	return ret;
}

int main()
{
	char arr[20] = { "hello " };
	printf("%s\n", _strcat(arr, "world"));
	system("pause");
	return 0;
}
  • strcmp

字符串比较,对应字符大小

int strcmp(const char *str1,const char *str2);

标准规定:

  1. 第一个字符串大于第二个字符串,则返回大于0的数字
  2. 第一个字符串等于第二个字符串,则返回0
  3. 第一个字符串小于第二个字符串,则返回小于0的数字
  • 模拟实现strcmp
#define _CRT_SECURE_NO_WARNINGS 1

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

int _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 *str1 = "abcdef";
	char *str2 = "abcd";
	printf("%d\n", _strcmp(str1, str2));
	system("pause");
	return 0;
}

长度受限制的字符串函数

  • strncpy

将源的第一个num字符复制到目标。如果结束源字符串C。在num字符被复制之前,会找到null字符(由null字符表示),然后用0填充目标字符串,直到将num字符的总数写到目的地为止

char* strncpy(char *destination, const char *source, size_t num);
  1. 拷贝num个字符从源字符串到目标空间
  2. 如果源字符串的长度小于num,则拷贝完源字符串之后,在目标的后面追加0直到num个
  • 模拟实现strncpy
#define _CRT_SECURE_NO_WARNINGS 1

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

char* _strncpy(char *dest,const char *src,size_t num)
{
	assert(dest&&src);
	char *start = dest;
	while (num && (*dest++ = *src++))//拷贝字符串
	{
		num--;
	}
	if (num)//用'\0'填充
	{
		while (--num)
		{
			*dest++ = '\0';
		}
	}
	return(start);
}

int main()
{
	char arr[] = "abcdefgh";
	char *p = "hello";
	printf("%s\n", _strncpy(arr, p, 5));
	system("pause");
	return 0;
}
  • strncat

  1. 附加源的第一个num字符到目标,加上一个终止的null字符。
  2. 如果源文件中C字符串的长度小于num,则只复制到终止null字符之前的内容。
char* strncat(char *destination, const char *source, size_t num);

举个例子:

#define _CRT_SECURE_NO_WARNINGS 1

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

int main()
{
	char str1[20];
	char str2[20];
	strcpy(str1, "To be ");
	puts(str1);
	strcpy(str2, "or not to be");
	puts(str2);
	strncat(str1, str2, 6);
	puts(str1);
	system("pause");
	return 0;
}
  • 模拟实现strncat
#define _CRT_SECURE_NO_WARNINGS 1

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

char* _strncat(char *dest,const char *src,size_t num)
{
	assert(dest&&src);
	char *ret = dest;
	while (*dest++)//先找到目标空间的结尾
	{
		;
	}
	dest--;//将'\0'留出来
	while (num--)
	{
		if (!(*dest++ = *src++))
		{
			return(ret);
		}
		*dest = '\0';
	}
	return(ret);
}

int main()
{
	char str1[20] = "hello ";
	char str2[]= "abcdefg";
	_strncat(str1, str2, 5);
	puts(str1);
	system("pause");
	return 0;
}
  • strncmp

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

标准规定:

  1. 第一个字符串大于第二个字符串,则返回大于0的数字
  2. 第一个字符串等于第二个字符串,则返回0
  3. 第一个字符串小于第二个字符串,则返回小于0的数字
  • 模拟实现strncmp
#define _CRT_SECURE_NO_WARNINGS 1

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

int _strncmp(const char *str1, const char *	str2, size_t count)
{
	assert(str1&&str2);
	while (count&&(*str1 == *str2))
	{
		if (*str1 == '\0')
		{
			return 0;
		}
		str1++;
		str2++;
		count--;
	}
	return *str1 - *str2;
}

int main()
{
	char *str1 = "abcdef";
	char *str2 = "abcdasdf";
	printf("%d\n", _strncmp(str1, str2, 5));
	printf("%d\n", strncmp(str1, str2, 5));
	system("pause");
	return 0;
}

字符串查找函数

  • strchr

返回指向C字符串str中第一个出现的字符的指针。

char * strchr ( const char *string, int ch);

例子:

#define _CRT_SECURE_NO_WARNINGS 1

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

int main()
{
	char *p = "hello world";
	char *ret = strchr(p, 'd');
	if (ret != NULL)
	{
		printf("%s\n", ret);
	}
	else
	{
		printf("找不到\n");
	}
	system("pause");
	return 0;
}
#define _CRT_SECURE_NO_WARNINGS 1

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

int main()
{
	char str[] = "This is a sample string";
	char * pch;
	printf("Looking for the 's' character in \"%s\"...\n", str);
	pch = strchr(str, 's');
	while (pch != NULL)
	{
		printf("found at %d\n", pch - str + 1);
		pch = strchr(pch + 1, 's');
	}
	system("pause");
	return 0;
}
  • 模拟实现strchr
#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>
#include <stdlib.h>

char *_strchr(const char *str, int ch)
{
	while (*str && *str != (char)ch)
	{
		str++;
	}

	if (*str == (char)ch)
	{
		return((char *)str);
	}
	return(NULL);
}

int main()
{
	char *p = "hello world";
	char *ret = _strchr(p, 'd');
	if (ret != NULL)
	{
		printf("%s\n", ret);
		printf("%d\n", ret - p + 1);
	}
	else
	{
		printf("找不到\n");
	}
	system("pause");
	return 0;
}
  • strrchr

返回指向C字符串str中最后出现的字符的指针。

char *strrchr(const char *str, int ch);

例子:

#define _CRT_SECURE_NO_WARNINGS 1

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

int main()
{
	char str[] = "This is a sample string";
	char * pch;
	pch = strrchr(str, 's');
	printf("Last occurence of 's' found at %d \n", pch - str + 1);
	system("pause");
	return 0;
}
  • strpbrk

返回一个指针,指向str2中的任何字符在str1中的第一个出现,如果没有匹配,则返回一个空指针。(返回第一个字符串中第一个出现在第二个字,符串中的字符的地址)

char * strpbrk(const char *, const char *);

例子:

#define _CRT_SECURE_NO_WARNINGS 1

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

int main()
{
	char str[] = "This is a sample string";
	char key[] = "aeiou";
	char * pch;
	printf("Vowels in '%s': \n", str);
	pch = strpbrk(str, key);
	while (pch != NULL)
	{
		printf("%c ", *pch);
		pch = strpbrk(pch + 1, key);
	}
	printf("\n");
	system("pause");
	return 0;
}
  • strstr

返回一个指向str2在strl中的第一次出现的指针,或者如果str2不是str1的一部分,则返回一个空指针。

char * strstr(const char *str1, const char *str2);

例子:

#define _CRT_SECURE_NO_WARNINGS 1

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

int main()
{
	char str[] = "This is a simple string";
	char *pch;
	pch = strstr(str, "simple");
	puts(pch);
	system("pause");
	return 0;
}
  • 模拟实现strstr
#define _CRT_SECURE_NO_WARNINGS 1

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

char *_strstr(const char *str1, const char *str2)
{
	assert(str1&&str2);
	const char *s1 = str1;
	const char *s2 = str2;
	const char *cur = str1;
	while (*cur)
	{
		s1 = cur;
		s2 = str2;
		while (*s1&&*s2 && (*s1 == *s2))
		{
			s1++;
			s2++;
		}
		if (*s2 == '\0')
		{
			return (char*)cur;
		}
		if (*s1 == '\0')
		{
			return NULL;
		}
		cur++;
	}
	return NULL;
}

int main()
{
	char str[] = "This is a simple string";
	char *pch;
	pch = _strstr(str, "simple");
	puts(pch);
	system("pause");
	return 0;
}

高级字符串查找函数

  • strspn

返回str1的初始部分的长度,它仅由str2的一部分字符组成。(这个函数返回的是str1字符串中开始部分匹配str2字符串中字符的个数)

size_t strspn(const char * str1, const char * str2);

例子:

#define _CRT_SECURE_NO_WARNINGS 1

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

int main()
{
	int i = 0;
	char str1[] = "129th";
	char str2[] = "1234567890";
	i = strspn(str1, str2);
	printf("The initial number has %d digits.\n", i);
	system("pause");
	return 0;
}
  • strcspn

扫描str1,查找属于str2的任何字符的第一次出现,返回第一次出现之前读取的str1字符的数量。(该函数返回str1指向字符串中开始部分出现的不在str2指向字符串中的字符个数)。

size_t strcspn ( const char * str1, const char * str2 );

例子:

#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
	char str1[] = "fcba73";
	char str2[] = "1234567890";
	int i = 0;
	i = strcspn(str1, str2);
	printf("The first number in str is at position %d.\n", i + 1);
	system("pause");
	return 0;
}
  • strtok

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

例子:

#define _CRT_SECURE_NO_WARNINGS 1

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

int main()
{
	char str[] = "- This, a sample string.";
	char * pch;
	printf("Splitting string \"%s\" into tokens:\n", str);
	pch = strtok(str, " ,.-");
	while (pch != NULL)
	{
		printf("%s\n", pch);
		pch = strtok(NULL, " ,.-");
	}
	printf("%s\n", str);//检查原字符串被改变
	system("pause");
	return 0;
}
#define _CRT_SECURE_NO_WARNINGS 1

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

int main()
{
	char *p = "[email protected]";
	const char* sep = ".@";
	char arr[30];
	char *str = NULL;
	strcpy(arr, p);//将数据拷贝一份,处理arr数组的内容
	for (str = strtok(arr, sep); str != NULL; str = strtok(NULL, sep))
	{
		printf("%s\n", str);
	}
	printf("%s\n", p);//检查原字符串未被改变
	system("pause");
	return 0;
}

错误信息报告函数

  • strerror

返回错误码,所对应的错误信息

char * strerror(int errnum);

例子:

#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>//必须包含头文件

int main()
{
	FILE * pFile;
	pFile = fopen("unexist.ent", "r");//没有此文件
	if (pFile == NULL)
		printf("Error opening file unexist.ent: %s\n", strerror(errno));
	//error:No such file or directory
	//errno: Last error number
	system("pause");
	return 0;
}
#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>//必须包含头文件

int main()
{
	int *p = (int *)malloc(INT_MAX);//开辟空间过大
	if (p == NULL)
	{
		printf("%s\n", strerror(errno));
		system("pause");
		return 0;
	}
	//...
	free(p);
	system("pause");
	return 0;
}

字符操作函数

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

例子:

字符转换:

#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int main()
{
	int i = 0;
	char str[] = "Test String.\n";
	char c;
	while (str[i])
	{
		c = str[i];
		if (isupper(c)) c = tolower(c);
		putchar(c);
		i++;
	}
	system("pause");
	return 0;
}

内存操作函数

  • memcpy

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

内存拷贝

#define _CRT_SECURE_NO_WARNINGS 1

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

void *_memcpy(void *dest, const void *src, size_t sz)
{
	
	assert(dest != NULL);
	assert(src != NULL);
	unsigned int i = 0;
	void *ret = dest;
	for (i = 0; i < sz; i++)
	{
		*(char*)dest = *(char*)src;
		dest=(char*)dest+1;
		src = (char*)src + 1;
	}
	return ret;
}

int main()
{

	int arr1[10] = { 0 };
	int arr2[] = { 1, 2, 3, 4, 5 };

	//char arr1[] = "abcdefghi";
	//int arr2[] = { 1, 0, 0, 0, 0 };
	_memcpy(arr1, arr2, 3 * sizeof(arr2[0]));
	system("pause");
	return 0;
}
  • memmove

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

内存拷贝,在memcpy基础上,自身内部可以实现拷贝

#define _CRT_SECURE_NO_WARNINGS 1

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

void *_memmove(void *dest, const void *src, size_t sz)
{
	void *ret = (char*)dest;
	if (dest < src)
	{
		//前---->后
		while (sz--)
		{
			*(char*)dest = *(char*)src;
			dest = (char*)dest + 1;
			src = (char*)src + 1;
		}
	}
	else
	{
		//后--->前
		while (sz--)
		{
			*((char*)dest+sz) = *((char*)src+sz);
		}
	}
	return ret;
}

int main()
{
	int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
	_memmove(arr, arr+3, 4 * sizeof(arr[0]));
	system("pause");
	return 0;
}
  • memcmp

比较从ptr1和ptr2指针开始的num个字节
标准规定:

  1. 第一个大于第二个,则返回大于0的数字
  2. 第一个等于第二个,则返回0
  3. 第一个小于第二个,则返回小于0的数字
int memcmp(const void *ptr1,const void *ptr2,size_t num);

例子:

#define _CRT_SECURE_NO_WARNINGS 1

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

int main()
{
	char buffer1[] = "DWgaOtP12df0";
	char buffer2[] = "DWGAOTP12DF0";
	int n;
	n = memcmp(buffer1, buffer2, sizeof(buffer1));
	if (n>0) printf("'%s' is greater than '%s'.\n", buffer1, buffer2);
	else if (n<0) printf("'%s' is less than '%s'.\n", buffer1, buffer2);
	else printf("'%s' is the same as '%s'.\n", buffer1, buffer2);
	system("pause");
	return 0;
}
  • memchr

void *memchr(const void *str, int ch, size_t num);

实例:

#define _CRT_SECURE_NO_WARNINGS 1

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

int main()
{
	char * pch;
	char str[] = "Example string";
	pch = (char*)memchr(str, 'p', strlen(str));
	if (pch != NULL)
		printf("'p' found at position %d.\n", pch - str + 1);
	else
		printf("'p' not found.\n");
	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41562665/article/details/81742424
今日推荐