C语言常用的字符,字符串,内存库函数的介绍及其实现( C语言从入门到入土(进阶篇))

简介: C语言常用的字符,字符串,内存库函数的介绍及其实现( C语言从入门到入土(进阶篇))

求字符串长度

strlen

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

strcpy

strcat

strcmp

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

strncpy

strncat

strncmp

字符串查找

strstr

strtok

错误信息报告

strerror

字符操作

内存操作函数

memcpy

memmove

memset

memcmp


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

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


函数介绍及其实现


1.1. 介绍strlen


image.png


字符串已经 '\0' 作为结束标志,strlen函数返回的是在字符串中 '\0' 前面出现的字符个数(不包含 '\0' )。

参数指向的字符串必须要以 '\0' 结束。

注意函数的返回值为size_t,是无符号的( 易错 )


1.2. 模拟实现strlen


三种方式


方式1

// 计数器方式
int my_strlen ( const char * str )
{
        int count = 0 ;
        while ( * str )
        {
        count ++ ;
        str ++ ;
        }
        return count ;
}


方式2

// 不能创建临时变量计数器
int my_strlen ( const char * str )
{
        if ( * str == '\0' )
        return 0 ;
        else
        return 1 + my_strlen ( str + 1 );
}

方式3

// 指针 - 指针的方式
int my_strlen ( char * s )
{
      char * p = s ;
      while ( * p != ‘\0’ )
              p ++ ;
      return p - s ;
}


2.1. 介绍strcpy


image.png


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.2.  模拟实现

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


2.3. strncpy


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.

就只是多了一个n,也就是往后比较几个字符,其余的和strcpy一样,这样可以减少strcpy的错误。  


3.1. 介绍strcat


image.png


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' 结束。


目标空间必须有足够的大,能容纳下源字符串的内容。


目标空间必须可修改。  


3.2. 模拟实现

char * my_strcat ( char * dest , const char* src )
{
        char * ret = dest ;
        assert ( dest != NULL );
        assert ( src != NULL );
        while ( * dest )
        {
                dest ++ ;
        }
//找到尾
        while (( * dest ++ = * src ++ ))
        {
                ;
        }
//这里和strcpy一样
        return ret ;
}


3.3. 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.(如果source中的字符串长度小于num,那么只复制到终止空字符之前的内容)


4.1. 介绍strcmp


image.png


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


4.2. 模拟实现

int my_strcmp ( const char * src , const char * dst )
{
        int ret = 0 ;
       assert ( src != NULL );
        assert ( dest != NULL );
        while ( ! ( ret = * ( unsigned char * ) src - * ( unsigned char * ) dst ) && * dst )
                ++ src , ++ dst ;
        if ( ret < 0 )
                ret = - 1 ;
        else if ( ret > 0 )
                ret = 1 ;
        return ( ret );
}


4.3. strncmp

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


比较到出现另个字符不一样或者一个字符串结束或者num个字符全部比较完。


image.png


5.1. 介绍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. (如果找到了子字符串,返回第一次遇到的地址,或者NULL)


image.png


5.2. 模拟实现

char *   strstr ( const char * str1 , const char * str2 )
{
        char * cp = ( char * ) str1 ;
        char * s1 , * s2 ;
        if ( !* str2 )
            return (( char * ) str1 );
        while ( * cp )
      {
                s1 = cp ;
                s2 = ( char * ) str2 ;
                while ( * s1 && * s2 && ! ( * s1 -* s2 ) )
                        s1 ++ , s2 ++ ;
                if ( !* s2 )
                        return ( cp );
                cp ++ ;
      }
        return ( NULL );
}


6.1. 介绍strtok


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


sep参数是个字符串,定义了用作分隔符的字符集合。


第一个参数指定一个字符串,它包含了0个或者多个由sep字符串中一个或者多个分隔符分割的标记。

strtok函数找到str中的下一个标记,并将其用\0 结尾,返回一个指向这个标记的指针。(注: strtok函数会改变被操作的字符串,所以在使用strtok函数切分的字符串一般都是临时拷贝的内容并且可修改。)

strtok函数的第一个参数不为 NULL ,函数将找到str中第一个标记,strtok函数将保存它在字符串中的位置。

strtok函数的第一个参数为 NULL ,函数将在同一个字符串中被保存的位置开始,查找下一个标记。

如果字符串中不存在更多的标记,则返回 NULL 指针。


image.png


6.2. 使用

/* 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 ;
}


image.png


7.1. 介绍strerror


char * strerror( int errnum);

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


image.png


7.2. 使用

/* 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 ;
}


image.png


8.1. 介绍memcpy


void * memcpy ( void * destination, const void * source, size_t num );


函数memcpy从source的位置开始向后复制num个字节的数据到destination的内存位置。

这个函数在遇到 '\0' 的时候并不会停下来。

如果source和destination有任何的重叠,复制的结果都是未定义的。


image.png


8.2. 使用


image.png


8.3. 模拟实现

void * memcpy ( void * dst , const void * src , size_t count )
{
        void * ret = dst ;
       assert ( dst );
        assert ( src );
        /*
        * copy from lower addresses to higher addresses
        */
        while ( count -- ) {
                * ( char * ) dst = * ( char * ) src ;
                dst = ( char * ) dst + 1 ;
                src = ( char * ) src + 1 ;
      }
        return ( ret );
}


9.1. 介绍memmove


void * memmove( void * destination, const void * source, size_t num);


和memcpy的差别就是memmove函数处理的源内存块和目标内存块是可以重叠的。

如果源空间和目标空间出现重叠,就得使用memmove函数处理。


image.png


9.2. 使用

#include <stdio.h>
#include <string.h>
int main ()
{
          char str [] = "memmove can be very useful......" ;
          memmove ( str + 20 , str + 15 , 11 );
          puts ( str );
          return 0 ;
}


image.png


10.1 介绍memcmp


int memcmp ( const void * ptr1 ,

                      const void * ptr2 ,

                      size_t num);

比较从ptr1和ptr2指针开始的num个字节 。


image.png


0.2. 使用

#include <stdi
#include <stri
int main ()
{
          char buffer1
          char buffer2
          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 );
          return 0 ;
}


今天的内容就到这里了哈!!!

要是认为作者有一点帮助你的话!

就来一个点赞加关注吧!!!当然订阅是更是求之不得!

赠人玫瑰,手有余香=。=!

最后的最后感谢大家的观看!!!

你们的支持是作者写作的最大动力!!!

下期见哈!!!

相关文章
|
10天前
|
C语言 C++
C语言 之 内存函数
C语言 之 内存函数
25 3
|
1天前
|
存储 缓存 C语言
【c语言】简单的算术操作符、输入输出函数
本文介绍了C语言中的算术操作符、赋值操作符、单目操作符以及输入输出函数 `printf` 和 `scanf` 的基本用法。算术操作符包括加、减、乘、除和求余,其中除法和求余运算有特殊规则。赋值操作符用于给变量赋值,并支持复合赋值。单目操作符包括自增自减、正负号和强制类型转换。输入输出函数 `printf` 和 `scanf` 用于格式化输入和输出,支持多种占位符和格式控制。通过示例代码详细解释了这些操作符和函数的使用方法。
17 10
|
5天前
|
存储 编译器 C语言
C语言函数的定义与函数的声明的区别
C语言中,函数的定义包含函数的实现,即具体执行的代码块;而函数的声明仅描述函数的名称、返回类型和参数列表,用于告知编译器函数的存在,但不包含实现细节。声明通常放在头文件中,定义则在源文件中。
ly~
|
10天前
|
数据可视化 BI API
除了 OpenGL,还有哪些常用的图形库可以在 C 语言中使用?
除了OpenGL,C语言中还有多个常用的图形库:SDL,适合初学者,用于2D游戏和多媒体应用;Allegro,高性能,支持2D/3D图形,广泛应用于游戏开发;Cairo,矢量图形库,支持高质量图形输出,适用于数据可视化;SFML,提供简单接口,用于2D/3D游戏及多媒体应用;GTK+,开源窗口工具包,用于创建图形用户界面。这些库各有特色,适用于不同的开发需求。
ly~
23 4
|
10天前
|
C语言
C语言函数
C语言函数
10 0
|
11天前
|
存储 安全 编译器
深入C语言库:字符与字符串函数模拟实现
深入C语言库:字符与字符串函数模拟实现
|
11天前
|
C语言 C++
c语言回顾-内存操作函数
c语言回顾-内存操作函数
34 0
|
2月前
|
存储 编译器 C语言
【C语言篇】数据在内存中的存储(超详细)
浮点数就采⽤下⾯的规则表⽰,即指数E的真实值加上127(或1023),再将有效数字M去掉整数部分的1。
260 0
|
1天前
|
存储
共用体在内存中如何存储数据
共用体(Union)在内存中为所有成员分配同一段内存空间,大小等于最大成员所需的空间。这意味着所有成员共享同一块内存,但同一时间只能存储其中一个成员的数据,无法同时保存多个成员的值。
|
5天前
|
存储 弹性计算 算法
前端大模型应用笔记(四):如何在资源受限例如1核和1G内存的端侧或ECS上运行一个合适的向量存储库及如何优化
本文探讨了在资源受限的嵌入式设备(如1核处理器和1GB内存)上实现高效向量存储和检索的方法,旨在支持端侧大模型应用。文章分析了Annoy、HNSWLib、NMSLib、FLANN、VP-Trees和Lshbox等向量存储库的特点与适用场景,推荐Annoy作为多数情况下的首选方案,并提出了数据预处理、索引优化、查询优化等策略以提升性能。通过这些方法,即使在资源受限的环境中也能实现高效的向量检索。