在不同的情況下,我有一個單詞 exit 的所有排列的陣列,有些時候它有效,有些時候它不起作用。我錯過了什么?
// Define all possible case permutations of exit
char *exitStrings[16] = {
"exit",
"exiT",
"exIt",
"eXit",
"exIT",
"eXiT",
"eXIt",
"eXIT",
"EXIT",
"Exit",
"EXit",
"ExIt",
"ExiT",
"EXiT",
"EXIt",
"ExIT"};
// read the message from client and copy it in buffer
read(sockfd, buff, sizeof(buff));
// if read message is one of the permuations of exit strings
// exit client
for (int i = 0; i < sizeof(*exitStrings); i )
{
if (strncmp(buff, exitStrings[i], 4) == 0)
{
printf("Client Exit and the Connection is still open\n");
printf("Listening for new client...\n");
}
}
蒂亞
uj5u.com熱心網友回復:
您可以將輸入轉換為小寫,然后將其與 "exit" 進行比較:
#include <ctype.h>
for(int i = 0; buff[i]; i )
{
buff[i] = tolower(buff[i]);
}
if (strncmp(buff, "exit", 5) == 0)
{
printf("Client Exit and the Connection is still open\n");
printf("Listening for new client...\n");
}
uj5u.com熱心網友回復:
似乎我的原始代碼沒有我最初計算的 16 個案例中的 1 個,我想出了如何在不區分大小寫的情況下進行比較。感謝您所有的幫助
#include <ctype.h>
// read the message from client and copy it in buffer
read(sockfd, buff, sizeof(buff));
if(strncasecmp(buff, "exit",4)==0)
{
printf("Client Exit and the Connection is still open\n");
printf("Listening for new client...\n");
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/469134.html
標籤:C
上一篇:制作學校管理系統并出現錯誤
下一篇:為什么Java多執行緒代碼死鎖