一个c语言练习的小问题(颠倒单词顺序,求指点)
RT我c新手,写了这样一个程序,题目要求是输入一串字符(exp:you can cage a swallow cant'you?),然后要求这样输出(exp:you cant’swallow a cage can you?),额按照提示思路是这样的,循环逐个读取字符,将其存入一个字符数组,当遇到‘?’,‘!’,‘.’终止循环,将终止符存入一个字符变量,再用一个循环反向搜索数组,找到最后一个单词起始位置显示最后一个单词,然后在反向搜索倒数第二个,依次继续,最后显示出终止的符号。
额,我实在太笨了,只有照葫芦画瓢写了如下:
#include<stdio.h>
void main()
{
char sentence[50];
char temp,temp_end;
int count=0,i=0,temp_count=0,j=0;
temp=getchar();
while(temp!='!' && temp!='?' && temp!='.' && temp!='\n')//语句终止符号
{
sentence[i]=temp;
i++;
temp=getchar();
}
sentence[i]=temp;
temp_end=temp;
count=i-1;
while(count>=0)
{
if(sentence[count]==' ')
{
temp_count=count-1;
for(j=count+1;j<=count;j++)
{
printf("%c",sentence[j]);
}
count=temp_count;
}
count--;
}
printf("%c",temp_end);
}
分数是少了点,我第一次发帖,请海涵。。求指点啊,调试不过啊,改了好几次了。