[問題] 指標指向字串時,判斷大小寫?

看板C_and_CPP (C/C++)作者 (千里之外)時間14年前 (2011/08/16 22:47), 編輯推噓2(2011)
留言13則, 8人參與, 最新討論串1/1
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) dev-c 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) 問題(Question): 已判斷字母的ascii code值符合小寫,但count_min值不會加1? 餵入的資料(Input): 預期的正確結果(Expected Output): 錯誤結果(Wrong Output): 小寫字數錯誤 程式碼(Code):(請善用置底文網頁, 記得排版) #include <stdio.h> #include <stdlib.h> int main(void){ char *ptr = "We Are Bast Friends."; char ch; int i=0; int count=0; int count_min=0; while((*(ptr+i))!= '\0'){ if(97<*(ptr+i)<122){ count_min++; } count++; i++; } printf("一共有%d個字元\n",count); printf("一共有%d個小寫字元\n",count_min); system("pause"); return 0; } 補充說明(Supplement): -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.46.112.3

08/16 22:48, , 1F
請寫 if ('a' <= *(ptr+i) && *(ptr+i) <= 'z')
08/16 22:48, 1F

08/16 22:49, , 2F
if(97<*(ptr+i)<122) 這什麼東西 XD
08/16 22:49, 2F

08/16 22:56, , 3F
判斷大小寫不要跟數字或字元比大小, 請愛用islower
08/16 22:56, 3F

08/16 22:57, , 4F
搭配isalpha
08/16 22:57, 4F

08/16 22:57, , 5F
之類ctype.h裡的函式或巨集
08/16 22:57, 5F

08/16 22:58, , 6F
理論上是函式實作上可能是巨集XDD
08/16 22:58, 6F

08/16 23:10, , 7F
if...
08/16 23:10, 7F

08/16 23:41, , 8F
其實只有一個重點就是 C/C++ 不能串不等式而已...
08/16 23:41, 8F

08/17 00:01, , 9F
是可以 只是意思會不一樣
08/17 00:01, 9F

08/17 00:16, , 10F
謝謝各位...
08/17 00:16, 10F

08/18 02:11, , 11F
標準只有說 0-9 是連續的... 請用 islower 之類的。
08/18 02:11, 11F

08/18 18:51, , 12F
人家都說是ascii code了...
08/18 18:51, 12F

08/22 04:58, , 13F
對不起我看太快沒看到 orz...
08/22 04:58, 13F
文章代碼(AID): #1EIeCE33 (C_and_CPP)
文章代碼(AID): #1EIeCE33 (C_and_CPP)