作者whiteshirts (MJIB)
看板Examination
标题[考题] 程式语言考题一问
时间Mon Dec 1 00:41:49 2014
出处: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://webptt.com/cn.aspx?n=bbs/Examination/M.1417365711.A.DBB.html
1F:推 CaTom: for(i=j;i<=0;i--) 中间项反了.. 12/01 01:05
2F:→ Mewra: 改成这样for (i = j; i >= 0; i--) 可能要多了解for loop 12/01 01:06
3F:→ EEERRIICC: 上面正解,让参数i从 j值递减到0 12/01 01:11
4F:→ whiteshirts: 了解 以上谢谢 12/01 10:42