#include <stdio.h>
#include <ctype.h>
int main() {
char str[100];
char out[] = "exit";
do {
printf("Enter a string: ");
scanf("%s", str);
// some if else statement here
} while (toupper(str[3]) != toupper(out[3]));
}
我把索引 3 放在那里,因為如果我把索引 0 放在那里,如果輸入的字串以字母 e 開頭,代碼將終止。我嘗試了while回圈,但它對我不起作用。另外,我想在輸入單詞“exit”后列印一條提示訊息“檢測到終止關鍵字”,然后終止回圈。
您還會注意到該toupper()功能。我在那里使用它是因為我希望我的回圈不區分大小寫,所以不管小寫或大寫或兩者的組合,當輸入單詞“exit”時回圈應該終止。
uj5u.com熱心網友回復:
toupper(str[3]) != toupper(out[3])str將比較and的第 4 個大寫字母out,因此回圈將迭代直到str[3]is 'T'。你想strcasecmp(str, out)改用。記得要#include <strings.h>。
uj5u.com熱心網友回復:
有多個問題:
isPalindrome()函式回傳0true是令人困惑的。為避免對負值
char的未定義行為,應將char引數轉換為.toupper(unsigned char)關鍵字的測驗
exit不正確。如果第四個字母是 at或 a則退出T。你應該使用strcasecmp來測驗這個exit詞。scanf("%s", str)如果用戶輸入的單詞超過 99 個位元組,則可能會出現未定義的行為。使用scanf("
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/476248.html上一篇:根據字串有條件地拆分列
