书中算法 经典用以记录
#include<stdio.h>
#define MAXLINE 1000
int getline(char line[],int maxline);
void reverse(char s[]);
int main()
{ char line[MAXLINE];
while((getline(line,MAXLINE))>0){ //读取每一行
reverse(line);
printf("%s",line);
}
}
int getline(char s[],int lim)
{ int c,i;
for(i=0;i<lim-1 && (c=getchar())!=EOF && c!='\n'; i++ )
s[i]=c;
if(c=='\n'){
s[i]=c;
i++;
}
s[i]='\0';
return i;
}
void reverse(char s[])
{ int i=0;
int j=0;
char temp;
while(s[i]!='\0') //去掉结尾字符
i++;
i--;
if(s[i]=='\n')
i--;
while(j<i){ //s[i]和s[j]交换
temp=s[j];
s[j]=s[i];
s[i]=temp;
j++;
i--;
}
}
函数getline获取每一行的字符具有应用价值