C语言字符串函数的详解和模拟实现

本篇文章讲围绕以下知识点进行讲解

求字符串长度
strlen

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

长度受限制的字符串函数介绍
strncpy strncat strncmp

字符串查找
strstr strtok

错误信息报告
strerror

字符操作 内存操作函数
memcpy memmove memset memcmp

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

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

strlen

该函数用来求字符串长度

size_t strlen ( const char * str );

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

#include <stdio.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; 
}

这段代码的结果是str2>str1,因为两者是无符号的,相减后还是大于0

strlen的模拟实现:

size_t my_strlen(const char* str)
{
	int count = 0;
	assert(str != NULL);
	while (*str != '\0')
	{
	count++;
	str++;
	}
	return count;
}

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’ 拷贝到目标空间。
目标空间必须足够大,以确保能存放源字符串。
目标空间必须可变。

strcpy的模拟实现:

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

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’ 结束。
目标空间必须有足够的大,能容纳下源字符串的内容。
目标空间必须可修改。

模拟实现:

char* my_strcat(char* dest, char* src)
{
	assert(dest && src);
	char* ret = dest;
	//找目标空间中的\0
	while (*dest)
	{
		dest++;
	}
	//拷贝
	while (*dest++ = *src++)
	{
		;
	}

	return ret;
}

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.
标准规定:
第一个字符串大于第二个字符串,则返回大于0的数字
第一个字符串等于第二个字符串,则返回0
第一个字符串小于第二个字符串,则返回小于0的数字

需要注意的是,该函数比较的不是长度,而是字符ascii码大小

模拟实现:

int my_strcmp(const char* s1, const char* s2)
{
	assert(s1 && s2);
	while (*s1 == *s2)
	{
		if (*s1 == '\0')
		{
			return 0;//相等
		}

		s1++;
		s2++;
	}
	//不相等
	if (*s1 > *s2)
		return 1;
	else
		return -1;
}

strncpy

该代码用于从一个字符串拷贝n个字符到另一个字符串上

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个。

strncat

该函数用于向一个字符串尾部追加n个另一个字符串中的字符

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.

使用方法如下:

/* strncat example */
#include <stdio.h>
#include <string.h>
int main () {
  char str1[20];
  char str2[20];
  strcpy (str1,"To be ");
  strcpy (str2,"or not to be");
  strncat (str1, str2, 6);
  puts (str1);
  return 0;
}

strncmp

将一个字符串和另一个字符串的前n项进行比较

int strncmp ( const char * str1, const char * str2, size_t num );

使用如下:

#include <stdio.h>
#include <string.h>
int main () {
  char str[][5] = { "R2D2" , "C3PO" , "R2A6" };
  int n;
  puts ("Looking for R2 astromech droids...");
  for (n=0 ; n<3 ; n++)
  if (strncmp (str[n],"R2xx",2) == 0)
  {
    printf ("found %s\n",str[n]);
  }
return 0; }

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.

模拟实现:

char* my_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;
		}
		cur++;
	}

	return NULL;//找不到
}

strtok

将字符串以自己给定的标识进行分割

char * strtok ( char * str, const char * sep );

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

/* strtok example */
#include <stdio.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, " ,.-");
  	}
	return 0; 
}

或者这样

#include <stdio.h>
int main() 
{
   	char *p = "zhangpengwei@bitedu.tech";
    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);
    }
    return 0;
}

strerror

该函数用于把错误码返回为一个字符串

char * strerror ( int errnum );

使用如下:

/* strerror example : error list */
#include <stdio.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));
    //errno: Last error number
	return 0; 
}

字符分类函数:

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

使用如下:

int tolower ( int c );
int toupper ( int c );
#include <stdio.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++; 
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值