C和指针之判断参数在关键字字符串列表中是否匹配
生活随笔
收集整理的這篇文章主要介紹了
C和指针之判断参数在关键字字符串列表中是否匹配
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1、題目
*判斷參數(shù)是否與一個關(guān)鍵字列表任何單詞匹配*并返回匹配的索引值,如果沒有找到返回-1
2、代碼實現(xiàn)
#include <stdio.h> #include <stdlib.h> #include <string.h>/***判斷參數(shù)是否與一個關(guān)鍵字列表任何單詞匹配*并返回匹配的索引值,如果沒有找到返回-1**/int lookup_keyword(const char *const desired, const char *keyword[], int const size) {const char **p;for (p = keyword; p < keyword + size; p++){if (strcmp(desired, *p) == 0){return p - keyword;}}return -1; }int main() {const char *keyword[] = {"chen", "gong", "yu", "cai"};const char *const desired = "yu";int size = sizeof(keyword) / sizeof(keyword[0]);printf("size is %d\n", size);int result = lookup_keyword(des總結(jié)
以上是生活随笔為你收集整理的C和指针之判断参数在关键字字符串列表中是否匹配的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C和指针之二维字符串数组用指针数组、数组
- 下一篇: C和指针之数组之编程练习2