看板 Examination 關於我們 聯絡資訊
出處:98年身障人員 三等考試 程式語言 考題:請撰寫一段程式執行以下工作,不限程式語言 輸入:字串 輸出:原字串順序顛倒(例如:輸入:abcd 輸出:dcba) 想法: 輸入字串個數-1,可得字元陣列最後之註標(j) 再用for迴圈依序取出倒數字元 寫法 #include <stdio.h> #include <string.h> int main(void) { chat str[100]; int i,j; printf("Input a string:"); scanf("%s",str); j=strlen(str)-1; printf("Your output is:"); for(i=j;i<=0;i--) printf("%c",str[i]); return 0; } 最後compile過了,但卻沒有正確輸出(Your output is:) 我試著用for(i=0;i<=j;i++)可以正確跑出輸入的字串 但上述方法卻不能跑出顛倒的字串 想請問,是哪裡有問題,該如何更正? 謝謝回覆 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 118.168.176.106 ※ 文章網址: http://www.ptt.cc/bbs/Examination/M.1417365711.A.DBB.html
CaTom: for(i=j;i<=0;i--) 中間項反了.. 12/01 01:05
Mewra: 改成這樣for (i = j; i >= 0; i--) 可能要多瞭解for loop 12/01 01:06
EEERRIICC: 上面正解,讓參數i從 j值遞減到0 12/01 01:11
whiteshirts: 了解 以上謝謝 12/01 10:42