首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

颠倒文本,

2012-10-21 
颠倒文本,求助。Sample Inputolleh !dlrowmI morf .udhI ekil .mcaSample Outputhello world!Im from hdu

颠倒文本,求助。
Sample Input 


olleh !dlrow
m'I morf .udh
I ekil .mca


Sample Output 


hello world!
I'm from hdu.
I like acm.

这我写的代码,不知为何在OJ系统上AC不了。还有没有更好的写法?

#include <stdio.h>
int main ()
{
char a[101];
int i=0,j=0;
gets(a);

while (a[i]!='\0')
{
if (a[i]==' ')
{ j=i-1;
while (a[j]!=' ')
{printf ("%c",a[j]);
if (j==0)
break;
j--;
}
printf (" ");
}
i++;
}
i=i-1;
while (a[i]!=' ')
{
printf ("%c",a[i]);
i--;
}
printf ("\n");
return 0;
}


[解决办法]

探讨

C/C++ code

#include <string.h>
#include <stdio.h>

char* strrev(char *str)
{
size_t len = strlen(str);
char* start = str;
char* end = str + len - 1;
char ch;

if(str != NULL)
……

热点排行