example of string searching function of strchr
strchr ---
語法 :
The strchr() function locates the first occurrence of
The function strchr() returns a pointer to the located character
*************************************/
#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 ---
語法 :
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
*************************************/
#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
語法:
說明:找出字串第一次出現的地方
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
*************************************/
#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
語法:
說明:
在字串s中尋找字串charset中任何一個字元相匹配的第一個字元的位置,空字元NULL不包括在內。返回指向s中第一個相匹配的字元的指標,如果沒有匹配字元則返回空指標NULL。
The
returns a pointer to the first occurrence of any character from
*************************************/
#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
Return an integer value specifying the length of the substring in
*************************************/
void main( void )
{
printf("%d\n",strspn ("answer is 4321", "abcde"));
printf("%d\n",strspn ("answer is 4321", "rewsna"));
//answer的相反
}

output:
1
6
/********************************
example of string searching function of strcspan
strcspn ---
語法:
說明:
傳回 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"));
}

output:
10
4
14
/********************************
example of string searching function of strtok
語法:
說明:
將字串str1 依字串str2 的值切開成小段小段的字串。
*************************************/
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
}
}
/**********************************************************
example of string searching function of strchr_r
語法:
就像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!