Linux C function() 參考手冊:strstr(在字串中查找(搜尋)指定的字串)

Linux C function() 參考手冊:strstr(在字串中查找(搜尋)指定的字串)

Linux C function() 參考手冊:strstr(在字串中查找(搜尋)指定的字串) 

資料來源:http://people.cs.nctu.edu.tw/~yslin/library/linuxc/main.htm
線上執行:http://www.tutorialspoint.com/compile_c_online.php
code2html:http://tohtml.com/

 

strstr(在一字符串中查找指定的字符串)
相關函數
index,memchr,rindex,strchr,strpbrk,strsep,strspn,strtok
表頭文件
#include<string.h>
定義函數
char *strstr(const char *haystack,const char *needle);
函數說明
strstr()會從字符串haystack 中搜尋字符串needle,並將第一次出現的地址返回。
返回值
返回指定字符串第一次出現的地址,否則返回0。
範例

 

 
#include <string.h>
#include <stdio.h>
int main()
{
char *s="0123456789876543210";
char *p='\0',*q='\0';
q= strstr(s,"987");
p= strstr(s,"901");
printf("%d\n",p-s);//沒有找到,值<0
printf("%d\n",q-s);//有找到,值>0
return 0;
}


 

 


發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *