當前位置:首頁 » 編程語言 » 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
}
}

熱點內容
聊天軟體編程 發布:2024-09-17 03:00:07 瀏覽:725
linuxoracle安裝路徑 發布:2024-09-17 01:57:29 瀏覽:688
兩個安卓手機照片怎麼同步 發布:2024-09-17 01:51:53 瀏覽:207
cf編譯後沒有黑框跳出來 發布:2024-09-17 01:46:54 瀏覽:249
安卓怎麼禁用應用讀取列表 發布:2024-09-17 01:46:45 瀏覽:524
win10設密碼在哪裡 發布:2024-09-17 01:33:32 瀏覽:662
情逢敵手迅雷下載ftp 發布:2024-09-17 01:32:35 瀏覽:337
安卓如何讓軟體按照步驟自動運行 發布:2024-09-17 01:28:27 瀏覽:197
Z包解壓命令 發布:2024-09-17 01:27:51 瀏覽:221
吉林ipfs存儲伺服器雲主機 發布:2024-09-17 01:27:38 瀏覽:685