史萊姆論壇

史萊姆論壇 (http://forum.slime.com.tw/)
-   程式語言討論區 (http://forum.slime.com.tw/f76.html)
-   -   [c語言]想讀出A-Z (ASCII 65-90), _(ASCII 95), a-z(ASCII 97-122)等字元, 忽略其他 (http://forum.slime.com.tw/thread200515.html)

bobo21 2007-03-20 07:39 PM

[c語言]想讀出A-Z (ASCII 65-90), _(ASCII 95), a-z(ASCII 97-122)等字元, 忽略其他
 
從指標裡讀字, 然後寫到螢幕上(stdout)

但我只想讀出 A-Z (ASCII 65-90), _(ASCII 95), a-z(ASCII 97-122) 等字元....忽略其它字元.

例如:
int main()
{
char *string = "Hello! Peter, how are you?"
read(string);
}
int read(FILE *input)
{
int ch = fgetc(input);
while(ch!=EOF)
{
printf("%c", ch);
ch = fgetc(input);
}
}

結果應該是 Hello Peter how are you
我現在只想到用一堆 if statement 來判斷 ch, 然後把correct 這變數當flag用...不過很亂又複雜.

int read(FILE *input)
{
int correct = 0;
int ch = fgetc(input);
while(ch!=EOF)
{
if(ch > '64' && ch < '91')
{correct = 1;}

else if(ch == '95')
{correct = 1;}

else if(ch > '96' && ch < '123')
{correct = 1;}

if(correct)
{
printf("%c", ch);
ch = fgetc(input);
correct = 0;
}
else
{ch = fgetc(input);}
}
}

請教大家不知是否有其他更好的邏輯.

wenneng 2007-03-24 10:40 AM

不用到那麼多if,一個就夠了
if((ch >= 'A' && ch <= 'Z') || (ch == '_') || (ch >= 'a' && ch <= 'z'))
不過字元才加單引號數值不用加單引號


所有時間均為台北時間。現在的時間是 07:33 AM

Powered by vBulletin® 版本 3.6.8
版權所有 ©2000 - 2024, Jelsoft Enterprises Ltd.

『服務條款』

* 有問題不知道該怎麼解決嗎?請聯絡本站的系統管理員 *


SEO by vBSEO 3.6.1