strchr函数

来源:互联网 发布:哪个网络播放器最好用 编辑:程序博客网 时间:2024/06/06 16:26
function
<cstring>

strchr

const char * strchr ( const char * str, int character );      char * strchr (       char * str, int character );
Locate first occurrence of character in string
Returns a pointer to the first occurrence of character in the C string str.

The terminating null-character is considered part of the C string. Therefore, it can also be located in order to retrieve a pointer to the end of a string.

Parameters

str
C string.
character
Character to be located. It is passed as its int promotion, but it is internally converted back to char for the comparison.

Return Value

A pointer to the first occurrence of character in str.
If the character is not found, the function returns a null pointer.

Portability

In C, this function is only declared as:

char * strchr ( const char *, int ); 

instead of the two overloaded versions provided in C++.

Example

1234567891011121314151617
/* strchr example */#include <stdio.h>#include <string.h>int main (){  char str[] = "This is a sample string";  char * pch;  printf ("Looking for the 's' character in \"%s\"...\n",str);  pch=strchr(str,'s');  while (pch!=NULL)  {    printf ("found at %d\n",pch-str+1);    pch=strchr(pch+1,'s');  }  return 0;}
Edit & Run


Output:
Looking for the 's' character in "This is a sample string"...found at 4found at 7found at 11found at 18
0 0
原创粉丝点击