练习1-19
编写函数reverse (s),将字符串s中的字符顺序颠倒过来。使用该函数编写一个程序,每次颠倒一个输入行中的字符顺序。
#include <stdio.h>
#include <string.h>
#define MAX_LINE 1000
int getline(char line[]);
//反转字符串
void reverse(char s[]);
int main()
{
char line[MAX_LINE] = {"7、Eventually, you'll learn to cry that on the inside.()"};
printf("反转前:\n%s\n",line);
printf("反转后:\n");
reverse(line);
//颠倒输入行顺序
char line2[MAX_LINE];
int c ;
int i = 0;
while((c = getchar())!= EOF)
{
if(c !='\n')
line2[i++] =c;
else
{
line2[i] = '\0';
reverse(line2);
line2[0]='\0';//清空
i =0;//重要的一步(导致数据老是叠加,出错)
}
}
getchar();
return 0;
}
//获取字符串长度(不包括\0)
int getline(char line[])
{
int i = 0;
while(line[i