當前位置:首頁 » 編程語言 » c語言strstr函數

c語言strstr函數

發布時間: 2024-08-24 08:06:26

『壹』 c語言中如何從一個字元串中查詢是否有給定的關鍵詞!

  1. strstr() 函褲巧數搜索一個字元串在另一個字元串中的第一次胡春鍵出現。找到所搜索的字元串,則該函數返回第一次匹配的字元串的地址;如果未找到所搜索的字元串,則返回NULL。
    包含文件:string.h
    函數名: strstr
    函數原型:
    extern char *strstr(char *str1, const char *str2);
    語法:
    strstr(str1,str2)
    str1: 被查找目標string expression to search.
    str2: 要查找對象The string expression to find.
    返回值:若str2是str1的子串,則先確定str2在str1的第一次出現的位置,並返回此str1在str2首位置的地址。;如果str2不是str1的子串,則返回NULL。

  2. 常式:

    #include<stdio.h>
    #include<string.h>
    intmain(){
    constchar*a="abcdefgh";
    constchar*b="abc";
    if(strstr(a,b)!=NULL)森液printf("found! ");
    elseprintf("notfound! ");
    return0;
    }

『貳』 C語言中如何查找字元串

用strstr這個函數

包含文件:string.h
函數名: strstr
函數原型:extern char *strstr(char *str1, char *str2);
功能:找出str2字元串在str1字元串中第一次出現的位置(不包括str2的串結束符)。
返回值:返回該位置的指針,如找不到,返回空指針。

源代碼:

#include<stdio.h>
#include<string.h>//調用string.h中的strstr函數
void main(){
char ch1[255]="abcde";
char ch2[100]="cd";
char* ch;//用於接受返回值
if((ch=strstr(ch1,ch2))==NULL){//說明沒有要找的字元串
printf("-1\n");
}else{//說明找到了那個字元串
printf("%d\n",ch-ch1+1);//cde的地址減去abcde的地址+1
}
}

熱點內容
linux怎麼編譯c文件 發布:2025-09-18 09:55:16 瀏覽:301
python安裝後無法運行 發布:2025-09-18 09:45:57 瀏覽:236
安卓手機怎麼剪輯音樂 發布:2025-09-18 09:44:16 瀏覽:781
伺服器地址修改在哪找 發布:2025-09-18 09:42:41 瀏覽:441
sntp伺服器地址 發布:2025-09-18 09:28:36 瀏覽:552
phpunit 發布:2025-09-18 09:25:19 瀏覽:571
怎麼改伺服器的ip地址嗎 發布:2025-09-18 09:24:33 瀏覽:12
編譯703n固件 發布:2025-09-18 08:50:59 瀏覽:539
三星手機系統文件夾是哪個文件夾 發布:2025-09-18 08:48:45 瀏覽:284
rmijava 發布:2025-09-18 08:38:26 瀏覽:22