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

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


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个字节
  • 返回值如下:


相关文章
|
1月前
|
算法 Linux C++
【Linux系统编程】深入解析Linux中read函数的错误场景
【Linux系统编程】深入解析Linux中read函数的错误场景
205 0
|
1月前
|
存储 并行计算 前端开发
【C++ 函数 基础教程 第五篇】C++深度解析:函数包裹与异步计算的艺术(二)
【C++ 函数 基础教程 第五篇】C++深度解析:函数包裹与异步计算的艺术
39 1
|
1月前
|
数据安全/隐私保护 C++ 容器
【C++ 函数 基础教程 第五篇】C++深度解析:函数包裹与异步计算的艺术(一)
【C++ 函数 基础教程 第五篇】C++深度解析:函数包裹与异步计算的艺术
47 0
|
1月前
|
存储 监控 Linux
【Linux IO多路复用 】 Linux下select函数全解析:驾驭I-O复用的高效之道
【Linux IO多路复用 】 Linux下select函数全解析:驾驭I-O复用的高效之道
54 0
|
3月前
Turndown 源码解析:一、辅助函数
Turndown 源码解析:一、辅助函数
35 0
|
1月前
|
C语言
c语言内存函数的深度解析
c语言内存函数的深度解析
32 3
|
3月前
|
存储 C语言
C 语言函数完全指南:创建、调用、参数传递、返回值解析
函数是一段代码块,只有在被调用时才会运行。 您可以将数据(称为参数)传递给函数。 函数用于执行某些操作,它们对于重用代码很重要:定义一次代码,并多次使用。
94 3
|
4月前
|
JSON JavaScript Go
Go 语言学习指南:变量、循环、函数、数据类型、Web 框架等全面解析
掌握 Go 语言的常见概念,如变量、循环、条件语句、函数、数据类型等等。深入了解 Go 基础知识的好起点是查阅 Go 官方文档
466 2
|
1月前
|
算法 编译器 C语言
【C++ 函数 基本教程 第六篇 】深度解析C++函数符号:GCC与VS的名称修饰揭秘
【C++ 函数 基本教程 第六篇 】深度解析C++函数符号:GCC与VS的名称修饰揭秘
42 1
|
1月前
|
算法 Serverless 数据安全/隐私保护
【C++ 函数 基本教程 第三篇 】深度解析C++函数类型:探寻全局函数、成员函数与静态函数的奥秘
【C++ 函数 基本教程 第三篇 】深度解析C++函数类型:探寻全局函数、成员函数与静态函数的奥秘
43 1

推荐镜像

更多