/********************************
example of string searching function of strchr
strchr ---   找出第一次出現的字元
語法 : string strchr (const char *s, int c)
The strchr() function locates the first occurrence of  c in the string pointed to by  s. The terminating NUL character is considered part of the string. If  c is `\0', strchr() locates the terminating `\0'.
The function strchr()  returns a pointer to the located character, or NULL if the character does not appear in the string.
*************************************/
#include <string.h>
void main( void )
{
char S1[20]="Hello World";
int ch='o';
printf("String : %s\n",S1);
printf( "Search char:\t%c\n", ch );
//strchr會return   pointer to the located character扣去本來字串的起始位置就出第幾個字了
printf("the first occurrence at positon %d\n",strchr(S1,ch)-S1); // 4
}
 
/********************************
example of string searching function of strrchr
strrchr ---   取得字元最後一次出現處到結尾的字串
語法 : string strrchr (const char *s, int c)
The strrchr() function locates the last occurrence of c (converted to a char) in the string s. If c is `\0', strrchr() locates the terminating `\0'.
The strrchr() function  returns a pointer to the character, or a null pointer if c does not occur anywhere in s.
*************************************/
#include <string.h>
void main( void )
{
char S1[20]="Hello World";
int ch='o';
printf("The String is %s\n",S1);
printf( "Search char:\t%c\n", ch );
printf("the last occurrence at positon %d\n",strrchr(S1,ch)-S1); // 7
}
 
/********************************
example of string searching function of strstr
語法:char * strstr(const char *big, const char *little);
說明: 找出字串第一次出現的地方
The strstr() function locates the first occurrence of the null-terminated string S2 in the null-terminated string S1.
Returns a pointer to the first occurrence of S2 in S1, or  NULL if  S2 does not appear in  S1. If  S2 points to a string of zero length, the function returns  S1.
*************************************/
#include <string.h>
void main( void )
{
char email[] = "rene@mail.jlps.hc.edu.tw"; 
printf("%s",strstr (email, "@"));//@mail.jlps.hc.edu.tw
}
和strchr不同的是strchr只能用char搜尋 ,strstr可以用*char搜尋
 
/********************************
example of string searching function of strpbrk
語法:char *strpbrk(const char *s, const char *charset);
說明:
在字串s中尋找字串charset中任何一個字元相匹配的第一個字元的位置,空字元NULL不包括在內。返回指向s中第一個相匹配的字元的指標,如果沒有匹配字元則返回空指標NULL。
The  strpbrk function returns a pointer to the first occurrence of a character in  string that belongs to the set of characters in  CharSet. The search does not include the terminating null character.
returns a pointer to the first occurrence of any character from  CharSet in  string, or a  NULL pointer if the two string arguments have no characters in common.
*************************************/
#include <string.h>
void main( void )
{
char S1[20] = "Hi , How Are You ?\n";
char S2[] = "AHU";
char *result;
printf( "Source string : %s\n", S1 );
printf( "char set is : %s \n", S2);
result = strpbrk( S1, S2 );
printf( "first find String : %s",result);
while ( result = strpbrk( result , S2 ))
{
printf( "Next New String : %s",result);
result++ ; //move to next char, WHY??
}
}
output :
Source string : Hi , How Are You ?

char set is : AHU
first find String : Hi , How Are You ?
Next New String : Hi , How Are You ?
Next New String : How Are You ?
Next New String : Are You ?
Press any key to continue
result++的原因:
因為result會指向"Hi, How Are You ?"中的H,那麼每次執行strpbrk也都會比對到同樣的位置,所以為了比對完的不再被比對,我們必須讓他"過期"。
 
/********************************
example of string searching function of strspn
strspn ---   找出比對到的最初部份的長度
語法:int strspn (string str1, string str2)
說明:
傳回 str1最初的部份的長度,這個部份是完全地由 str2中的字元所構成的。
The  strspn function returns the index of the first character in string that does not belong to the set of characters in  CharSet. The search does not include terminating null characters.
Return an integer value specifying the length of the substring in  string that consists entirely of characters in  CharSet. If string begins with a character not in  CharSet, the function returns 0. No return value is reserved to indicate an error.
*************************************/
void main( void )
{
printf("%d\n",strspn ("answer is 4321", "abcde"));
printf("%d\n",strspn ("answer is 4321", "rewsna"));
//answer的相反
}
index.1.gif
output:
1
6
 
/********************************
example of string searching function of strcspan
strcspn ---   找出沒有比對到的最初部份的長度
語法:int strcspn (string str1, string str2)
說明:
傳回 str1最初的部份的長度,這個部份是不包含任何 str2中的字元。
*************************************/
void main( void )
{
     printf("%d\n",strcspn ("answer is 4321", "1234"));
     printf("%d\n",strcspn ("answer is 4321", "cde"));
     printf("%d\n",strcspn ("answer is 4321", "789"));
}
index.1.gif
output:
10
4
14
 
/********************************
example of string searching function of strtok
語法:int strtok (string str1, string str2)
說明: 
將字串str1 依字串str2 的值切開成小段小段的字串 首次調用時,str1必須指向要分解的字串,隨後調用要把str1設成NULL。strtok在str1中查找包含在str2中的字元並用NULL('\0')來替換,直到找遍整個字串。返回指向下一個標記串。當沒有標記串時則返回空字元NULL。
*************************************/
void main( void )
{
char string[] = "A string\tof ,,tokens\nand some more tokens";
char seps[] = " ,\t\n";
char *token;
printf( "%s\n\nTokens:\n", string );
/* Establish string and get the first token: */
token = strtok( string, seps );
while( token != NULL )
{
/* While there are tokens in "string" */
printf( " %s\n", token );
/* Get next token: */
token = strtok(  NULL, seps );
}
}
 
/**********************************************************
example of string searching function of strchr_r
語法:char * strtok_r (char *newstring, const char *delimiters, char **save_ptr)
就像strtok函數一樣,strtok_r函數能夠連續調用,以將一個字串分解成為幾個token。strtok_r函數不會破壞這個函數的狀態。因為它提供了一個指向不同函數的**save_ptr指標。
這個函數是POSIX.1b提出的建議,它被許多系統支援,以提供多線程的字元分解。
The strtok_r() function is a reentrant version of strtok(). The context pointer last must be provided on each call. strtok_r() may also be used to nest two parsing loops within one another, as long as separate context pointers are used.

The strtok() and strtok_r() functions return a pointer to the beginning
of each subsequent token in the string, after replacing the token itself
with a NUL character. When no more tokens remain, a null pointer is
returned.
*********************************************************/
int main(void)
{
     char test1[80], test2[80];
     char *sep = "\\/:;=-. ";
     char *word1, *word2, *brk1, *brk2;
     strcpy(test1, "THIS;IS. TEST/1!");
     for (word1=strtok_r(test1,sep,&brk1);word1;word1=strtok_r(NULL,sep,&brk1))
     {
          strcpy(test2, "this;is. test/2!");
          for (word2=strtok_r(test2,sep,&brk2);word2;word2=strtok_r(NULL,sep,&brk2))
          {
               printf("So far we're at %s:%s\n", word1, word2);
          }
     }
}

output:
So far we're at THIS:this
So far we're at THIS:is
So far we're at THIS:test
So far we're at THIS:2!
So far we're at IS:this
So far we're at IS:is
So far we're at IS:test
So far we're at IS:2!
So far we're at TEST:this
So far we're at TEST:is
So far we're at TEST:test
So far we're at TEST:2!
So far we're at 1!:this
So far we're at 1!:is
So far we're at 1!:test
So far we're at 1!:2!