Write a function reverse(s) that reverses the character string s.Use it to write a program tat reverses its input a line at a time.
#include<stdio.h>
#define MAXLINE 1000void reversed(char s[]);
int getlines(char line[],int maxline);
/*print the reversed of the input line */
main()
{
char line[MAXLINE];
while(getlines(line,MAXLINE) > 0){
reversed(line);
printf("%s",line);
}
}
int getlines(char s[],int lim)
{
int c,i;
for(i=0;i<lim-1 && (c= getchar()) != EOF && c!='\n';++i)
s[i]= c;
++i;
}
s[i] = '\0';
return i;
}
/*reverse:reverse the input line*/
void reversed(char s[])
{
int i,j;
char temp;
i = 0;
while(s[i] != '\0')
++ i;
- -i;
if ( s[i] == '\n')
- -i;
j = 0;
while(j < i)
{
temp = s[j];
s[j] = s[i];
s[i] = temp;
- - i;
++ j;
}
}
reverse.c
缺少红色字体的那段代码后,出现的错误结果
……………………………………………………………………………………………………………………………………………………………………………………………………………
/tmp/ccaYcNfA.o: In function `main':reverse.c:(.text+0x4e): undefined reference to `getlines'
collect2: ld returned 1 exit status
这些让我意外发现:这是一个常见的问题,解决办法:参考文章《静态库和共享库的定位搜索路径》
事实上,我的问题应该不属于这一范畴,只是因为写的都是这类程序,所以漏写了getlines函数。加上以后,运行成功。