C语言中怎么一次性的输入多行(不确定是多少行的)字符串,然后把它们全反过来输出,比如输入
wo shi da ge
ni shi xiao di
...
输出
...
id oaix ihs in
eg ad ihs ow
求大神指教指教!本人不胜感激!!!
#include <stdio.h>
#include <stdlib.h>
int main()
{
char* buf = NULL; //动态分配 //char buf[1024]; //指定长度
char ch;
int len = 0,i;
printf("Please input some line:\n");
while(scanf("%c",&ch)==1 && ch!=EOF && ch!='#'){ //定义一个结束符或者ctrl+z/d
//动态的实现
buf = (char*)realloc(buf,sizeof(char)*(len+1));
buf[len++] = ch;
//buf[len++] = ch; //指定长度
}
printf("reverse your input to print:\n");
for(i=len-1;i>=0;i--){
printf("%c",buf[i]);
}
free(buf); //动态清除
return 0;
}
追问容我再研究研究,嘿嘿,谢啦!
首先,非常感谢! 但容我再研究研究,
能直接写个代码让我具体研究下不?大神!
追答#define N 1024
#define M 256
void main()
{ char input[N][M];
int i,j,n,nn;
n=0;
while ( n=0;i-- )
{ nn=0; while ( input[i][nn] ) nn++;
for ( j=nn-1;j>=0;j-- ) printf("%c",input[i][j]);
printf("\n");
}
}
我全懂了
本回答被提问者采纳