接着(三)我们继续来看看String结构体内的各种函数如何实现。我们先看看char char_at(String* thisptr, int index)函数如何实现,源代码如下:
/**
* 获得指定位置的字符
* @param str 字符串结构体指针
* @param index 位置索引
* @return 指定索引的字符,如果索引越界,返回\0
*/
char char_at(String* thisptr, int index){
// 结构体指针不能为NULL, 索引要在范围内
if(thisptr == NULL || index < 0 || index >= thisptr->length){
return '\0';
}
return thisptr->value[index];
}
该函数是取字符串中指定索引的字符,在String* build_string(const char* str)函数中,我们已经为String*的结构体成员value在堆中分配了空间,并将str复制到value中,因此,我们首先判断一下,
thisptr是否为NULL,并且index是否在有效范围内,也就是index不能小于0,并且不能大于或等于thisptr的长度,如果thisptr为NULL或index不在有效范围内,那么返回'\0';
如果不为NULL且index在有效范围内,那么我们直接通过数组表示法,获取index所在的char即可。也就是 this.ptr->value[index];
int index_of(String* thisptr, char ch)函数,该函数获取给定的char第一次出现在字符串中的索引,如果不存在那么返回-1,源代码如下:
/**
* 获取字符串中第一次出现的指定字符的索引
*
* @param str 字符串结构体指针
* @param ch 指定字符
* @return 第一次出现的索引,如果没有找到,返回-1
*/
int index_of(String* thisptr, char ch){
if(thisptr == NULL){
return -1;
}
// 循环每个字符,当找到指定字符时,返回索引
for(int i = 0; i < thisptr->length; i++){
if(thisptr->value[i] == ch){
return i;
}
}
return -1;
}
主要实现思路循环判断value中的每个字符,如果与参数ch中的字符相等,那么就返回索引。
int last_index_of(String* thisptr, char ch)函数使找到最后一个ch的索引,与index_of相比区别在于last_index_of会一直循环到value的最后一个元素为止。
/**
* 获取字符串中最后一次出现的指定字符的索引
*
* @param thisptr 字符串结构体指针
* @param ch 指定字符
* @return 最后一次出现的索引,如果没有找到,返回-1
*/
int last_index_of(String* thisptr, char ch){
if(thisptr == NULL){
return -1;
}
// 循环每个字符,当找到指定字符时,返回索引
int tmpindex = -1;
for(int i = 0; i < thisptr->length; i++){
if(thisptr->value[i] == ch){
tmpindex = i;
}
}
return tmpindex;
}
int index_of_str(String* thisptr, const char* chptr)函数会获取字符串中第一次出现的指定字符串的索引,该函数会调用核心函数static int index_of_str_from_static(const char* source, const char* target, int fromIndex)函数。
/**
* 获取字符串中第一次出现的指定字符串的索引
*
* @param thisptr 字符串结构体指针
* @param chptr 指定字符串
* @return 第一次出现的索引,如果thisptr为NULL或chptr为NULL没有找到,返回-1
*/
int index_of_str(String* thisptr, const char* chptr){
if(thisptr == NULL || chptr == NULL){
return -1;
}
return index_of_str_from_static(thisptr->value, chptr, 0);
}
int last_index_of_str(String* thisptr, const char* chptr)函数获取字符串中最后一次出现的指定字符串的索引,该函数也会调用核心函数static int index_of_str_from_static(const char* source, const char* target, int fromIndex)函数,但会通过循环调用不断地去获取chptr的第一个字符的索引,直到其为-1为止。
/**
* 获取字符串中最后一次出现的指定字符串的索引
*
* @param thisptr 字符串结构体指针
* @param chptr 指定字符串
* @return 最后一次出现的索引,如果thisptr为NULL或chptr为NULL没有找到,返回-1
*/
int last_index_of_str(String* thisptr, const char* chptr){
if(thisptr == NULL || chptr == NULL){
return -1;
}
int pos = 0; // 初始搜索的位置,从0开始
int end = -1; // 搜索结果
int prev = -1; // 上一次搜索到的位置
while(1){
end = index_of_str_from_static(thisptr->value, chptr, pos);
if(end != -1){
prev = end;
pos = end + strlen(chptr);
}else{
break;
}
}
return prev;
}
序号 | 函数 | 简介 |
---|---|---|
1 |
int count_alnum(String* thisptr) |
计算字符串中字母和数字的数量 |
2 |
int count_alpha(String* thisptr) |
计算字符串中字母的数量 |
3 |
int count_digit(String* thisptr) |
计算字符串中数字的数量 |
4 |
int count_space(String* thisptr) |
计算字符串中空格的数量 |
以上函数会委托给ctype.h中的相应函数去判断给定的char是否满足相应的条件,给出int count_alnum(String* thisptr)的源码:
// 计算字符串中字母的数量
int count_alpha(String* thisptr){
char* tmp = thisptr->value;
int length = thisptr->length;
int count = 0;
for(int i = 0; i < length; i++){
if(isalpha(tmp[i])){
count++;
}
}
return count;
}
<ctype.h>
是C标准库中的一个头文件,提供了一些用于字符处理的函数,以下是一些常见的函数介绍:
- **
isalnum()
**:检查字符是否为字母或数字。 - **
isalpha()
**:检查字符是否为字母。 - **
iscntrl()
**:检查字符是否为控制字符。 - **
isdigit()
**:检查字符是否为十进制数字。 - **
isgraph()
**:检查字符是否有图形表示法。 - **
islower()
**:检查字符是否为小写字母。 - **
isprint()
**:检查字符是否为可打印字符。 - **
ispunct()
**:检查字符是否为标点符号字符。 - **
isspace()
**:检查字符是否为空白字符。 - **
isupper()
**:检查字符是否为大写字母。 - **
isxdigit()
**:检查字符是否为十六进制数字。 - **
tolower()
**:将大写字母转换为小写字母。 - **
toupper()
**:将小写字母转换为大写字母。
这些函数可以帮助你在C程序中进行字符的分类和转换操作。
int str_count(String* thisptr, const char* chptr)函数计算子串chptr在String结构体中出现的数量。该函数也会调用核心函数static int index_of_str_from_static(const char* source, const char* target, int fromIndex),返回值是chptr出现的数量。
/**
* 记录子串的数量
*
* @param thisptr 字符串结构体指针
* @param chptr 指定字符串
* @return 子串的数量
*/
int str_count(String* thisptr, const char* chptr){
if(thisptr == NULL || chptr == NULL){
return 0;
}
int pos = 0; // 初始搜索的位置,从0开始
int end = -1; // 搜索结果
int size = 0; // 子串的个数
while(1){
end = index_of_str_from_static(thisptr->value, chptr, pos);
if(end != -1){
pos = end + strlen(chptr);
size++; // 记录子串的个数
}else{
break;
}
}
return size;
}
index_of_str_from_static函数后续会详细讲解,String结构体中的一些函数调用此函数。
本章先介绍这么多函数,后续章节会逐步将String结构体中的所有函数都介绍一遍。
敬请期待!