C語言strchr函式

2021-07-07 02:30:22 字數 2759 閱讀 4756

strchr函式原型:extern char *strchr(const char *s,char c);查詢

字串s中首次出現字元c的位置。

char *strchr(const char* _str,int _val)

char *strchr(char* _str,int _ch)

標頭檔案:#include

功能:查詢

字串s中首次出現字元c的位置

說明:返回首次出現c的位置的

指標,返回的位址是被查詢字串指標開始的第乙個與val相同字元的指標,如果s中不存在c則返回

null。

返回值:成功則返回要查詢字元第一次出現的位置,失敗返回null

返回: 返回字串的一部分或者 

false(如果未發現 needle)。

例子:1

2

3

4

5

6

7

<?php

$email='[email protected]';

$domain=strchr($email,'@');

echo$domain;//列印@example.com

$user=strchr($email,'@',true);//從php5.3.0起

echo$user;//列印name

?>

實現:1

2

3

4

5

6

7

8

char*strchr(char*s,charc)

return*s==c?s:null;

}

舉例1:(在

visual c++ 6.0中執行通過)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

#include

#include

intmain(void)

執行結果:

the character r is at position: ring

請按任意鍵繼續. . .

舉例2:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

// strchr.c

#include

#include

intmain()

執行結果:not found!press any key to continue

舉例3:

1

2

3

4

5

6

7

8

9

10

11

#include

#include

voidmain()

fgets不會像gets那樣自動地去掉結尾的\n,所以程式中手動將\n位置處的值變為\0,代表輸入的結束。

C語言strchr 函式

char strchr const char str,int c 在引數str所指向的字串中搜尋第一次出現字元c 乙個無符號字元 的位置。下面是 strchr 函式的宣告。char strchr const char str,int c 該函式返回在字串 str 中第一次出現字元 c 的位置,如果未...

C語言中strchr 和index

在ubuntu下rindex 函式使用沒有問題,在mingw下會報錯,undefined reference to rindex warning implicit declaration of function rindex 經查詢了解如下 strchr 是c標準庫的一部分。index 是a now...

sprintf 函式和strchr 函式

可以用sprintf 函式把資訊輸出到字串,用法和printf fprintf類似,但應保證字串足夠大。strchr 函式 查詢字串s中首次出現字元c的位置,成功則返回要查詢字元第一次出現的位置,失敗返回null include include include include include inc...