字符函数和字符串函数解析及模拟实现

本文涉及的产品
云解析DNS,个人版 1个月
全局流量管理 GTM,标准版 1个月
云解析 DNS,旗舰版 1个月
简介: 字符函数和字符串函数解析及模拟实现


1. 求字符串长的函数

1.1strlen

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

1.2 strlen()模拟实现

//模拟实现的strlen()函数命名位my_strlen()函数
//模拟实现方法一
//size_t my_strlen(const char* str)
//{
//  assert(str);
//  int count = 0;
//  while (*str != '\0')
//  {
//    count++;
//    str++;
//  }
//  return count;
//}

//模拟实现方法二:指针-指针
//size_t my_strlen(const char* str)
//{
//  assert(str);
//  char* start = str;
//  while (*str)
//  {
//    str++;
//  }
//  return str - start;
//}

//模拟实现方法三:递归
size_t my_strlen(const char* str)
{
  assert(str);
  if (*str == '\0')
    return 0;
  else
    return 1 + my_strlen(str + 1);
}

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

2.1strcpy

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

2.1.2 模拟实现

//模拟实现的strcpy()函数命名位my_strcpy()函数
char* my_strcpy(char* dest, const char* src)
{
  assert(dest && src);
  char* ret = dest;
  while (*dest++ = *src++)
  {
    ;
  }
  return ret;
}

2.2 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’结束。
  • 会将源字符串中的’\0’拷贝到目标空间。
  • 目标空间必须足够大,能容纳下源字符串中的内容。
  • 目标空间必须可修改。

2.2.1 模拟实现

char* my_strcat(char* dest, const char* src)
{
  assert(dest && src);
  char* ret = dest;
  //1.找到目标空间'\0'
  while (*dest)
  {
    dest++;
  }
  //2. 追加字符
  while (*dest++ = *src++)
  {
    ;
  }
  return ret;
}

2.3 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的数字。

2.3.1 模拟实现

//模拟实现的strcmp()函数命名位my_strcmp()函数
int my_strcmp(const char* str1, const char* str2)
{
  assert(str1 && str2);
  while (*str1 == *str2)
  {
    str1++;
    str2++;
  }
  return *str1 - *str2;
}



3. 长度受限制的字符串函数

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

3.2 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.

3.3 strncmp

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

4. 字符串查找

4.1 strstr

 const 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.

4.1.2模拟实现

//模拟实现的strstr()函数命名位my_strstr()函数
char* my_strstr(const char* str1, const char* str2)
{
  assert(str1 && str2);
  char* cp = str1;
  char* s1 = cp;
  char* s2 = str2;
  
  if (*s2 == '\0')
    return NULL;
  while (*cp)
  {
    //开始匹配
    s1 = cp;
    s2 = str2;
    while(*s1 && *s2 && *s1 == *s2)
    {
      s1++;
      s2++;
    }
    if (*s2 == '\0')
      return cp;
    cp++;
  }
  return NULL;
}

4.2 strtok

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

4.2.1 如何使用

int main()
{
  char arr1[] = "zhenyuWang@yeah.net";
  char copy[30];
  strcpy(copy, arr1);

  char arr2[] = "@.";
  char* ret = NULL;
  for (ret = strtok(copy, arr2); ret != NULL; ret = strtok(NULL, arr2))
  {
    printf("%s\n", ret);
  }
  return 0;
}

5. 错误信息报告

库函数在执行时候,发生错误会将一个错误码存放在errno这个变量中。而errno是C语言提供的一个全局变量

5.1 strerror

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

5.1.1 如何使用

#include <stdio.h>
#include <string.h>
int main()
{
  for (int i = 0; i < 10; i++)
  {
    printf("%d: %s\n", i,strerror(i));
  }
  return 0;
}

运行结果:

字符分类函数:

字符转换:

int tolower (int c)
int toupper (int c)

6. 内存操作函数

6.1 memcpy

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

6.1.1 模拟实现

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;
  }
  return ret;
}

6.2 memmove

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

6.2.1 模拟实现

void* my_memmove(void* dest, const void* src, size_t num)
{
  assert(dest && src);
  void* ret = dest;
  if (dest > src)//从后向前拷贝
  {
    while (num--)
    {
      *((char*)dest + num) = *((char*)src + num);
    }
  }
  else//从前向后拷贝
  {
    while (num--)
    {
      *(char*)dest = *(char*)src;
      dest = (char*)dest + 1;
      src = (char*)src + 1;
    }
  }
  return ret;
}

6.3 memcmp

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


相关文章
|
2天前
|
存储 SQL Python
`urllib.parse`模块是Python标准库`urllib`中的一个子模块,它提供了处理URL(统一资源定位符)的实用功能。这些功能包括解析URL、组合URL、转义URL中的特殊字符等。
`urllib.parse`模块是Python标准库`urllib`中的一个子模块,它提供了处理URL(统一资源定位符)的实用功能。这些功能包括解析URL、组合URL、转义URL中的特殊字符等。
6 0
|
26天前
|
Shell 开发者
Shell 函数深入解析与实践
了解 Shell 函数的基础,包括定义、参数传递及返回值。函数定义有多种语法,如 `function func() {...}` 或 `func() {...}`。参数通过 `$1`, `$2` 等访问,`$@` 代表所有参数。`return` 用于返回退出状态码(0-255),非数值数据需用 `echo`。正确获取函数返回值应立即检查 `$?`,例如:`result=$?`。实践中不断探索和学习!
14 1
|
28天前
|
机器学习/深度学习 人工智能 算法
【机器学习】深度探索:从基础概念到深度学习关键技术的全面解析——梯度下降、激活函数、正则化与批量归一化
【机器学习】深度探索:从基础概念到深度学习关键技术的全面解析——梯度下降、激活函数、正则化与批量归一化
29 3
|
28天前
|
SQL 自然语言处理 前端开发
【JavaScript】ECMAS6(ES6)新特性概览(一):变量声明let与const、箭头函数、模板字面量全面解析
【JavaScript】ECMAS6(ES6)新特性概览(一):变量声明let与const、箭头函数、模板字面量全面解析
18 2
|
1月前
|
存储 算法 数据可视化
深入解析力扣161题:相隔为 1 的编辑距离(逐字符比较与动态规划详解)
深入解析力扣161题:相隔为 1 的编辑距离(逐字符比较与动态规划详解)
|
1月前
|
存储 算法 数据可视化
深入解析力扣157题:用Read4高效读取N个字符(多种解法与详细图解)
深入解析力扣157题:用Read4高效读取N个字符(多种解法与详细图解)
|
17天前
|
JavaScript 前端开发
jQuery 常用函数解析
jQuery 常用函数解析
14 0
|
17天前
|
C语言
C语言实现猜数字游戏:代码详解与函数解析
C语言实现猜数字游戏:代码详解与函数解析
12 0
|
18天前
|
域名解析 存储 监控
函数计算产品使用问题之多个函数如何指到同一个域名解析
函数计算产品作为一种事件驱动的全托管计算服务,让用户能够专注于业务逻辑的编写,而无需关心底层服务器的管理与运维。你可以有效地利用函数计算产品来支撑各类应用场景,从简单的数据处理到复杂的业务逻辑,实现快速、高效、低成本的云上部署与运维。以下是一些关于使用函数计算产品的合集和要点,帮助你更好地理解和应用这一服务。
|
20天前
|
存储 JavaScript 前端开发
JavaScript——JavaScript基础:数组 | JavaScript函数:使用、作用域、函数表达式、预解析
在JavaScript中,内嵌函数可以访问定义在外层函数中的所有变量和函数,并包括其外层函数能访问的所有变量和函数。①全局变量:不在任何函数内声明的变量(显式定义)或在函数内省略var声明的变量(隐式定义)都称为全局变量,它在同一个页面文件中的所有脚本内都可以使用。函数表达式与函数声明的定义方式几乎相同,不同的是函数表达式的定义必须在调用前,而函数声明的方式则不限制声明与调用的顺序。③块级变量:ES 6提供的let关键字声明的变量称为块级变量,仅在“{}”中间有效,如if、for或while语句等。
35 0

推荐镜像

更多