2010-05-07 82 views

回答

5

您可以使用

if (!strncmp("GET ", str, 4) 
{ 
    ... 
} 
else if (!strncmp("POST ", str, 5)) 
{ 
    ... 
} 
else 
{ 
    ... 
} 
+0

當然他需要strncmp。 strcmp(「GET」,「GET LOST」)不會返回0. – bmargulies 2010-05-07 18:04:53

1

使用STRNCMP與參數字符串1,字符串,n,其中字符串1和字符串是字符串進行比較和n個字符數。如果字符串匹配,則strncmp返回0或<如果string1按字典順序小於string2,則返回0;如果string2小於string1,則返回> 0。例子:

#include <string.h> 
... 
strncmp(somestring, "GET ", 4) == 0 
strncmp(somestring, "POST ", 5) == 0 
+1

當輸入匹配時,strncmp返回0。 – MSN 2010-05-07 17:39:11

+0

我知道,對不起忘了寫它。 – George 2010-05-08 09:47:43

1
#include <stdio.h> 
#include <string.h> 

typedef enum httpmethod 
{ 
    HTTP_ERR, 
    HTTP_GET, 
    HTTP_POST, 
    HTTP_METHOD_COUNT 

} httpmethod; 

const char* http_method_str[HTTP_METHOD_COUNT + 1] = 
{ 
    "UNKNOWN", 
    "GET ", 
    "POST ", 
    0 
}; 


httpmethod str_get_http_method(const char* str) 
{ 
    if (!str || strlen(str) < 4) 
     return HTTP_ERR; 

    const char* ptr[HTTP_METHOD_COUNT]; 
    int i; 
    int failcount = 0; 

    for (i = 1; i < HTTP_METHOD_COUNT; ++i) 
     ptr[i] = http_method_str[i]; 

    while (*str != '\0' && failcount < HTTP_METHOD_COUNT - 1) 
    { 
     for (i = 1; i < HTTP_METHOD_COUNT; ++i) 
     { 
      if (ptr[i] && *str != *ptr[i]++) 
      { 
       ptr[i] = 0; 
       ++failcount; 
      } 
     } 
     str++; 
    } 

    for (i = 1; i < HTTP_METHOD_COUNT; ++i) 
     if (ptr[i]) 
      return i; 

    return HTTP_ERR; 
} 


int main(int argc, char** argv) 
{ 
    const char* test[4] = { "GET ", "POST ", "GIT ", "PAST " }; 

    httpmethod result = HTTP_ERR; 

    int i; 

    for (i = 0; i < 4; ++i) 
    { 
     printf("checking str: %s\n", test[i]); 
     result = str_get_http_method(test[i]); 
     printf("result is type: %s\n", http_method_str[result]); 
    } 

    return 0; 
} 
+0

您可能需要在初始化'http_method_str []'結尾時使用NULL。 – 2010-05-07 20:11:59

+0

@Tim:那麼枚舉告訴你多少,並添加一個NULL需要一個進一步的數組索引,但我認爲這樣做不會有什麼壞處:-) – 2010-05-07 20:42:33

3

你不使用strncmp()時,你只需要幾個字符串之間進行區分:

#include <stdio.h> 
#include <stdint.h> 
#include <string.h> 

static uint32_t method_hash(const char *key) 
{ 
     int len; 
     uint32_t hash; 
     int i; 

     len = strlen(key); 
     for (hash = 0, i = 0; i < len; i++) { 
       hash += (unsigned int) key[i]; 
       hash += (hash << 10); 
       hash ^= (hash >> 6); 
     } 
     hash += (hash << 3); 
     hash ^= (hash >> 11); 
     hash += (hash << 15); 
     return hash; 
} 

int main(int argc, char *argv[]) 
{ 
    if (argc < 2) { 
     printf("Usage: %s <method>\n", argv[0]); 
     return 0; 
    } 

    switch(method_hash(argv[1])) { 
    case 802187597: 
     printf("Its GET\n"); 
     break; 
    case 740659500: 
     printf("Its POST\n"); 
     break; 
    default: 
     printf("Its RUBBISH\n"); 
     return 1; 
    } 

    return 0; 
} 

只要注意,哈希是不衝突的證據,但適合瞭解GET和POST的區別。我經常在字典中使用那個小寶石,只有當我認爲我只有一個電話strncmp()

我張貼這個答案告訴你,有很多種方法來處理字符串,在希望你避免代碼看起來像這樣,你繼續學習C:

if (! strncmp(string, "FOO ", 4)) { 
    do_this(); 
} else if (! strncmp(string, "BAR ", 4)) { 
    do_that(); 
} else if (! strncmp(string, "FOOBAR ", 7)) { 
    do_both(); 
/* ... madness ensues through 200 more lines and 100 more else if's ... */ 
} else { 
    return 0; 
} 

我的例子是不完全正確。如果希望代碼是可移植的,您希望在運行時確定散列值,而不是僅僅插入已知值。這是讀者的練習(提示,開關箱需要常量)。

+0

好的回答.... – 2010-05-07 20:46:10

+1

但你有沒有注意到空間? – 2010-05-07 20:48:52

+0

只是在你的例子中用一大串'else if(!strncmp(string ...'語句)來評論,在這種情況下,我認爲一個字符串和函數指針表是一個更清晰的解決方案。一個'str_begins()'類型的函數不需要長度參數 – tomlogic 2010-05-07 23:38:38

相關問題