C 語言標準函數庫分類導覽 - string.h strcspn()

string.h 的函數 strcspn() ,需要兩個字串當作參數,計算經過幾個字元會在第一個參數的字串遇到屬於第二個參數字串中的字元。



以下程式印出經過幾個字元在字串 s 中遇到屬於字串 t 的字元
#include <stdio.h>
#include <string.h>

int main(void)
{
    char s[] = "The spirit is willing but the flesh is weak.";
    char t[] = "but";
    
    printf("%d\n", strcspn(s, t));
    
    return 0;    
}

/* 《程式語言教學誌》的範例程式
    http://pydoing.blogspot.com/
    檔名:cstrcspn.c
    功能:示範 string.h 中函數 strcspn() 的使用
    作者:張凱慶
    時間:西元2010年6月 */


編譯後執行,結果如下



您可以繼續參考


沒有留言: