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

来源:互联网 发布:网络运营公司简介 编辑:程序博客网 时间:2024/06/08 11:14

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


原创粉丝点击