#include <stdio.h>#include <string.h>//strtok函數聲明在此頭文件中#include <stdlib.h>char const whitespace[] = " ";//以空格來隔開int main(){ char buffer[101]; int count = 0; //gets(buffer);//vs2015不支持該函數 gets_s(buffer,30);//C11標準定義的新的函數,其中第二參數就是允許的輸入長度,會自動添加'/0',輸入的字符長度加上'/0'后不能超過該參數的數值 char *Word; for (word = strtok(buffer, whitespace);word != NULL;word = strtok(NULL, whitespace))//char *strtok(char s[], const char *delim);參數s指向欲分割的字符串,參數delim則為分隔符字符(串) //在第一次調用時,strtok()必須給予參數s字符串,往后的調用則將參數s設置成NULL。每次調用成功則返回指向被分割出片段的指針 { if (strcmp(word, "the") == 0) count += 1; } PRintf("%d/n", count); return 0;}
|
新聞熱點
疑難解答