C语言 字符串函数
- 一、字符串长度函数 strlen
- 二、不受限制的字符串函数
- 2.1 复制字符串函数 strcpy
- 2.2 字符串连接函数 strcat
- 2.3 字符串比较函数strcmp
- 三、长度受限制的字符串函数
- 3.1 复制字符串函数 strncpy
- 3.2 字符串连接函数 strncat
- 3.3 比较字符串函数 strncmp
- 四、 字符串查找函数
-
前言:字符串是一种重要的数据类型,但是
C语言没有显式的字符串数据类型,字符串通过
字符串常量或
字符数组方式储存。C语言提供了许多与字符串相关函数,可以在头文件
<string.h>
中查看函数声明,本章将会
自行编写相关字符串函数
一、字符串长度函数 strlen
功能:字符串以 ‘\0’ 作为结束标志,strlen函数返回的是在字符串中 ‘\0’ 前面出现的字符个数(不包
含 ‘\0’ )
库函数strlen函数声明:
size_t strlen(char const *string);
返回值 :无符号整形,返回字符串长度
string:字符串起始位置指针
模拟实现
size_t my_strlen(const char* string)
{
assert(string);
size_t len = 0;
while (*string++ != '\0')
{
len++;
}
return len;
}
警告:
由于strlen返回类型是size_t类型即一个无符号整数类型,无符号整数绝不可能为负。在表达式中使用无符号整数可能造成不可预料后果
例1
#include <stdio.h>
#include <assert.h>
size_t my_strlen(const char* string)
{
assert(string);
size_t len = 0;
while (*string++ != '\0')
{
len++;
}
return len;
}
int main()
{
char* c1 = "hello";
char* c2 = "hello world";
if (my_strlen(c1) - my_strlen(c2) >= 0)
{
printf("c1 > c2\n");
}
else
{
printf("c1 < c2\n");
}
return 0;
}
输出
c1 > c2
二、不受限制的字符串函数
有些字符串函数是“不受限制的”,也就是说它们只是通过寻找字符串结束标志’\0’来判断字符串长度,在使用这些函数时,程序员必须保证字符串不会溢出所在内存
2.1 复制字符串函数 strcpy
功能:将源指向的字符串复制到目标指向的数组中,包括’\0’,并返回目标数组起始地址
库函数strlcpy函数声明:
char * strcpy ( char * destination, const char * source );
返回值:返回目标数组起始地址
destination:目标数组起始地址
source:源字符串起始地址
模拟实现
char * my_strcpy ( char * destination, const char * source )
{
assert(destination && source);
char* des = destination;
while ((*destination++ = *source++) != '\0')
{
;
}
return des;
}
警告:
1. 如果source与destination指向空间在内存中出现重叠,结果是未定义的
2. 如果源字符串比目标数组长,多余的字符仍将复制,将会出现非法访问内存的问题
2.2 字符串连接函数 strcat
功能:
将源字符串的副本追加到目标字符串尾部。目标字符串中的’\0’被源字符串的第一个字符覆盖,并且在目标字符串中由这两个字符串联而成的新字符串的末尾添加’\0’
库函数strcat声明:
char * strcat ( char * destination, const char * source );
返回值:返回目标字符串起始地址
destination:目标字符串起始地址
source:要追加的字符串起始地址
模拟实现
char * my_strcat ( char * destination, const char * source )
{
assert(destination && source);
char* des = destination;
while (*destination != '\0')
{
destination++;
}
while ( (*destination++ = *source++) != '\0')
{
;
}
return des;
}
例1:将c2字符串追加到c1字符串中
#include <stdio.h>
#include <assert.h>
char * my_strcat ( char * destination, const char * source )
{
assert(destination && source);
char* des = destination;
while (*destination != '\0')
{
destination++;
}
while ( (*destination++ = *source++) != '\0')
{
;
}
return des;
}
int main()
{
char c1[20] = "Hello";
char c2[20] = "World";
printf("%s\n",my_strcat(c1, c2));
return 0;
}
输出
HelloWorld
警告:
1. 如果source与destination指向空间在内存中出现重叠,结果是未定义的
2.程序员必须保证目标字符数组剩余空间能够保存整个源字符串
2.3 字符串比较函数strcmp
功能:
对两个字符串的每一个对应字符逐一比较,直到发现不相等为止。标准规定:第一个字符串大于第二个字符串,则返回大于0的数字
。第一个字符串等于第二个字符串,则返回0。第一个字符串小于第二个字符串,则返回小于0的数字。
库函数strcmp声明
int strcmp ( const char * str1, const char * str2 );
str1:第一个字符串起始地址
str2:第二个字符串起始地址
模拟实现
int my_strcmp(const char* str1, const char* str2)
{
assert(str1 && str2);
while (*str1 == *str2)
{
if (*str1 == '\0')
return 0;
str1++;
str2++;
}
return *str1 - *str2;
}
警告:
1.初学者常常会写if(strcmp(str1,str2))
表达式,以为两个字符串相等,它的结果将是真。但是,这个结果恰恰相反,因为两个字符串相等返回0
2.一个常见错误是以为第一个字符串大于第二个字符串返回1,小于则返回-1
三、长度受限制的字符串函数
标准库包含一些受限制的函数,这些函数接受一个显式的长度参数,用于限定进行复制或比较的字符数,防止难以预料的非法访问内存
3.1 复制字符串函数 strncpy
功能:将源字符串len个字符复制到目标字符串。如果strlen(source)小于len,destination用额外的’\0’填充到len长度。如果strlen(source)大于等于len,那么只有len个字符复制到destination中
库函数strncpy声明
char * strncpy ( char * destination, const char * source, size_t num );
返回值:返回目标数组起始地址
destination:目标数组起始地址
source:源字符串起始地址
num:需要复制字符个数
模拟实现
char* my_strncpy(char* destination, const char* source, size_t num)
{
assert(destination && source);
char* des = destination;
while (num)
{
num--;
if ((*destination++ = *source++) != '\0')
;
else
break;
}
while (num)
{
num--;
*destination++ = '\0';
}
return des;
}
警告:
1. 如果source与destination指向空间在内存中出现重叠,结果是未定义的
2. 当strlen(source)大于等于len并且strlen(destination)<=len,destination将不会以’\0’结尾
3.2 字符串连接函数 strncat
功能:从字符串source的开头拷贝n 个字符到destination字符串尾部,并追加’\0’字符。destination要有足够的空间来容纳要拷贝的字符串。如果n大于字符串source的长度,那么仅将src指向的字符串内容追加到dest的尾部。字符追加完成后,再追加’\0’。
库函数strncat声明
char * strncat ( char * destination, const char * source, size_t num );
返回值:返回目标字符串起始地址
destination:目标字符串起始地址
source:要追加的字符串起始地址
num:要追加字符个数
模拟实现
char* my_strncat(char* destination, const char* source, size_t num)
{
assert(destination && source);
char* des = destination;
while(*destination != '\0')
{
destination++;
}
while (num)
{
num--;
if ((*destination = *source) != '\0')
{
destination++;
source++;
}
else
{
break;
}
}
*destination = '\0';
return des;
}
3.3 比较字符串函数 strncmp
功能:比较两个字符串,但最多比较len个字节。如果两个字符串前len个字符相等返回0,str1大于str2返回大于0值,str1小于str2返回小于0的值
库函数strncmp声明
int strncmp ( const char * str1, const char * str2, size_t num );
str1:第一个字符串起始地址
str2:第二个字符串起始地址
num:比较len个字节(字符)
模拟实现
int my_strncmp(const char* str1, const char* str2, size_t num)
{
assert(str1 && str2);
while (num--)
{
if (*str1 == *str2)
{
str1++;
str2++;
}
else
{
return *str1 - *str2;
}
}
return 0;
}
四、 字符串查找函数
4.1 查找一个子字符串函数 strstr
功能:
在str1查找整个str2第一次出现位置,并返回一个指向该位置的指针。如果str2没有完整地出现在str1的任何地方,函数返回NULL指针。如果str2是一个空字符串,函数返回str1指针
库函数声明
const char * strstr ( const char * str1, const char * str2 );
返回值:str1中查找整个str2第一次出现位置或NULL
str1:需要扫描的字符串
str2:需要查找字符串(子串)
模拟实现
const char* my_strstr(const char* str1, const char* str2)
{
assert(str1 && str2);
const char* p1 = str1;
if (*str2 == '\0')
return p1;
while(*p1 != '\0')
{
const char* p2 = str2;
while (*p1 != *p2 && *p1 != '\0')
{
p1++;
}
if (*p1 == NULL)
{
return NULL;
}
const char* tmp = p1;
while (*tmp == *p2 && *p2 != '\0')
{
tmp++;
p2++;
}
if (*p2 == '\0')
return p1;
p1++;
}
return NULL;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)