[考題] 程式語言考題一問

看板Examination作者 (MJIB)時間11年前 (2014/12/01 00:41), 編輯推噓1(103)
留言4則, 4人參與, 最新討論串1/1
出處: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

12/01 01:05, , 1F
for(i=j;i<=0;i--) 中間項反了..
12/01 01:05, 1F

12/01 01:06, , 2F
改成這樣for (i = j; i >= 0; i--) 可能要多瞭解for loop
12/01 01:06, 2F

12/01 01:11, , 3F
上面正解,讓參數i從 j值遞減到0
12/01 01:11, 3F

12/01 10:42, , 4F
了解 以上謝謝
12/01 10:42, 4F
文章代碼(AID): #1KUqZFsx (Examination)