Pointers on C——9 Strings, Characters, and Bytes.6

9.5 Basic String Searching


There are many functions in the library that search strings in various ways. This wide variety of tools gives the C programmer great flexibility.

标准库中存在许多函数,它们用各种不同的方法查找字符串。这些各种各样的工具给了C 程序员很大的灵活性。


9.5.1 Finding a Character


The easiest way to locate a specific character in a string is with the strchr and strrchr functions, whose prototypes are:

在一个字符串中查找一个特定字符最容易的方法是使用strchr 和strrchr 函数,它们的原型如下所示:


char *strchr( char const *str, int ch );

char *strrchr( char const *str, int ch );


Note that the second argument is an integer. It contains a character value, however.strchr searches the string str to find the first occurrence of the character ch. Then a pointer to this position is returned. If the character does not appear at all in the string,a NULL pointer is returned. strrchr works exactly the same except that it returns a pointer to the last (rightmost) occurrence of the character.

注意它们的第2 个参数是一个整型值。但是,它包含了一个字符值。strchr 在字符串str中查找字符ch 第1 次出现的位置,找到后函数返回一个指向该位置的指针。如果该字符并不存在于字符串中,函数就返回一个NULL 指针。strrchr 的功能和strchr基本一致,只是它所返回的是一个指向字符串中该字符最后一次出现的位置(最右边那个)。


Here is an example;

这里有个例子:


char string[20] = "Hello there, honey.";

char *ans;

ans = strchr( string, 'h' );


ans will get the value string + 7 because the first 'h' appears in this position. Note that case is significant.

ans 所指向的位置将是string+7 ,因为第1 个‘h’ 出现在这个位置。注意这里大小写是有区别的。


9.5.2 Finding Any of Several Characters


The strpbrk function is more general. Instead of searching for one specific character, it looks for the first occurrence of any of group of characters. Its prototype is:

strpbrk是个更为常见的函数。它并不是查找某个特定的字符,而是查找任何一组字符第1 次在字符串中出现的位置。它的原型如下:


char *strpbrk( char const *str, char const *group );


This function returns a pointer to the first character in str that matches any of the characters in group, or NULL if none matched.

这个函数返回一个指向str 中第1 个匹配group 中任何一个字符的字符位置。如果未找到匹配,函数返回一个NULL 指针。


In the following code fragment,

在下面的代码段中,


char string[20] = "Hello there, honey.";

char *ans;

ans = strpbrk( string, "aeiou" );


ans will get the value string + 1 because this position is the first that contains any of the characters in the second argument. Once again, case is significant.

ans 所指向的位置是string+1 ,因为这个位置是第2 个参数中的字符第1 次出现的位置。和前面一样,这个函数也是区分大小写的。


9.5.3 Finding a Substring


To locate a substring, strstr is used. Its prototype is:

为了在字符串中查找一个子串,我们可以使用strstr 函数,它的原型如下:


char *strstr( char const *s1, char const *s2 );


This function finds the first place in s1 where the entire string s2 begins and returns a pointer to this location. If s2 does not appear in its entirety anywhere in s1, then NULL is returned. If the second argument is an empty string, then s1 is returned.

这个函数在sl 中查找整个s2第1 次出现的起始位置,并返回一个指向该位置的指针。如果s2 并没有完整地出现在sl 的任何地方,函数将返回一个NULL 指针。如果第2 个参数是一个空字符串,函数返回sl 。


The standard library includes neither a strrstr nor a strrpbrk function, but they are easy to implement if you need them. Program 9.2 shows one way of implementing strrstr. The same technique could be used for strrpbrk.

标准库中并不存在strrstr 或strrpbrk函数。不过,如果你需要它们,它们是很容易实现的。程序9.2显示了一种实现strrstr 的方法。这个技巧同样也可以用于实现strrpbrk 


/*

** Look in the string s1 for the rightmost occurrence of the string

** s2, and return a pointer to where it begins.

*/

#include <string.h>

char *

my_strrstr( char const *s1, char const *s2 )

{

register char *last;

register char *current;

/*

** Initialize pointer for the last match we've found.

*/

last = NULL;

/*

** Search only if the second string is not empty. If s2 is

** empty, return NULL.

*/

if( *s2 != '\0' ){

/*

** Find the first place where s2 appears in s1.

*/

current = strstr( s1, s2 );

/*

** Each time we find the string, save the pointer to

** where it begins. Then look after the string for

** another occurrence.

*/

while( current != NULL ){

last = current;

current = strstr( last + 1, s2 );

}

}

/*

** Return pointer to the last occurrence we found.

*/

return last;

}

Program 9.2 Find rightmost occurrence of a substring                      mstrrstr.c

查找子串最右一次出现的位置


上一章 Pointers on C——9  Strings, Characters, and Bytes.5


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SQLAlchemy 是一个 SQL 工具包和对象关系映射(ORM)库,用于 Python 编程语言。它提供了一个高级的 SQL 工具和对象关系映射工具,允许开发者以 Python 类和对象的形式操作数据库,而无需编写大量的 SQL 语句。SQLAlchemy 建立在 DBAPI 之上,支持多种数据库后端,如 SQLite, MySQL, PostgreSQL 等。 SQLAlchemy 的核心功能: 对象关系映射(ORM): SQLAlchemy 允许开发者使用 Python 类来表示数据库表,使用类的实例表示表中的行。 开发者可以定义类之间的关系(如一对多、多对多),SQLAlchemy 会自动处理这些关系在数据库中的映射。 通过 ORM,开发者可以像操作 Python 对象一样操作数据库,这大大简化了数据库操作的复杂性。 表达式语言: SQLAlchemy 提供了一个丰富的 SQL 表达式语言,允许开发者以 Python 表达式的方式编写复杂的 SQL 查询。 表达式语言提供了对 SQL 语句的灵活控制,同时保持了代码的可读性和可维护性。 数据库引擎和连接池: SQLAlchemy 支持多种数据库后端,并且为每种后端提供了对应的数据库引擎。 它还提供了连接池管理功能,以优化数据库连接的创建、使用和释放。 会话管理: SQLAlchemy 使用会话(Session)来管理对象的持久化状态。 会话提供了一个工作单元(unit of work)和身份映射(identity map)的概念,使得对象的状态管理和查询更加高效。 事件系统: SQLAlchemy 提供了一个事件系统,允许开发者在 ORM 的各个生命周期阶段插入自定义的钩子函数。 这使得开发者可以在对象加载、修改、删除等操作时执行额外的逻辑。
SQLAlchemy 是一个 SQL 工具包和对象关系映射(ORM)库,用于 Python 编程语言。它提供了一个高级的 SQL 工具和对象关系映射工具,允许开发者以 Python 类和对象的形式操作数据库,而无需编写大量的 SQL 语句。SQLAlchemy 建立在 DBAPI 之上,支持多种数据库后端,如 SQLite, MySQL, PostgreSQL 等。 SQLAlchemy 的核心功能: 对象关系映射(ORM): SQLAlchemy 允许开发者使用 Python 类来表示数据库表,使用类的实例表示表中的行。 开发者可以定义类之间的关系(如一对多、多对多),SQLAlchemy 会自动处理这些关系在数据库中的映射。 通过 ORM,开发者可以像操作 Python 对象一样操作数据库,这大大简化了数据库操作的复杂性。 表达式语言: SQLAlchemy 提供了一个丰富的 SQL 表达式语言,允许开发者以 Python 表达式的方式编写复杂的 SQL 查询。 表达式语言提供了对 SQL 语句的灵活控制,同时保持了代码的可读性和可维护性。 数据库引擎和连接池: SQLAlchemy 支持多种数据库后端,并且为每种后端提供了对应的数据库引擎。 它还提供了连接池管理功能,以优化数据库连接的创建、使用和释放。 会话管理: SQLAlchemy 使用会话(Session)来管理对象的持久化状态。 会话提供了一个工作单元(unit of work)和身份映射(identity map)的概念,使得对象的状态管理和查询更加高效。 事件系统: SQLAlchemy 提供了一个事件系统,允许开发者在 ORM 的各个生命周期阶段插入自定义的钩子函数。 这使得开发者可以在对象加载、修改、删除等操作时执行额外的逻辑。
Here is an example implementation of a function to compute the integral of any unary function using C language and pointers: ```c #include <stdio.h> double integral(double (*f)(double), double a, double b, int n) { double h = (b - a) / n; // width of each subinterval double sum = 0.0; // initialize the sum int i; for (i = 0; i < n; i++) { double x1 = a + i * h; double x2 = a + (i + 1) * h; double y1 = (*f)(x1); double y2 = (*f)(x2); double area = (y1 + y2) * h / 2.0; sum += area; } return sum; } // example usage double square(double x) { return x * x; } int main() { double a = 0.0; double b = 1.0; int n = 1000; double result = integral(&square, a, b, n); printf("Integral of x^2 from %g to %g with %d subintervals: %g\n", a, b, n, result); return 0; } ``` In this example, the `integral` function takes four arguments: a pointer to the unary function to integrate (`f`), the lower and upper bounds of integration (`a` and `b`, respectively), and the number of subintervals to use in the approximation (`n`). The function then computes the width of each subinterval (`h`), initializes the sum to 0, and iterates over the subintervals, computing the area under the curve for each and adding it to the sum. The final result is the sum of all the subinterval areas, which is returned by the function. To demonstrate the usage of this function, the `square` function is defined as an example of a unary function to integrate, and is passed as a pointer to the `integral` function along with the bounds of integration and number of subintervals. The result is printed to the console.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值