近期在贴吧上看到一则关于单词逆序输出的程序 比如把“I love China" 输出为”China love I"
对于这个问题 我认真思考了下,我的思路是:在一串字符串数据数据中 以一个空白字符为一个分割点 程序记录分割点的数量 并分别记录分割点的位置 把它们记入到一个数组中 后期让程序访问这个数组中的数据 数组里最后一个数据值为开始值 然后一直自减 直到结束 .
这是我的代码,但这个程序有个问题:
输入的数据最开始至少需要连个空白字符 输出才能按要求输出 这是我一直尝试解决的问题 却找不到办法 还希望各位给小弟指出更好的意见:
程序代码:/* 单词逆序输出程序 */
#include <stdio.h>
#include <ctype.h>
#include <conio.h>
#include <windows.h>
void word_down(char *);
void main()
{
char *date=" I love China";//注意 前两个数据是空白字符 两个空格
char input_date[100];
system("color 0E");
printf("单词逆序输出程序:\n");
printf("Sur date:\n");
printf("%s\n",date);
word_down(date);
printf("please input any string again:");
gets(input_date);
word_down(input_date);
printf("please input any key exit: ");
getch();
}
void word_down(char *src)//单词逆序输出
{
int i=0,c=0;
int x=0,y=0;//数组中每两个数据
int Temp_date[30];//临时数据 用来记录空白字符的位子
while(src[i]!='\0'){
if(!isalpha(src[i]))//用if语句 不能用while 会出错
c++;
Temp_date[c]=i;//记录每个空白字符的位置
i++;
}
printf("Down Word !\n");//逆序后的单词
while(c--){
x=Temp_date[c];
y=Temp_date[c+1];
while(x++<y)
printf("%c",src[x+1]);//需要注意的是 这里要 +1 否则输出会出问题
}
printf("\n");
}
这是小弟的愚见 还希望各位大神 能给小弟指出解决的办法 关于这个程序代码 也希望能指出更好的意见 供小弟学习 谢谢!!









嘿嘿 连续失败两次了