#include <stdio.h>
#define MAXLINE 1000
void reverse(char s[]);
int getline(char s[], int lim);
main()
...{
int len;
char line[MAXLINE];

while((len = getline(line, MAXLINE)) > 0) ...{
printf("%s", line);
reverse(line);
printf("%s", line);
}
return 0;
}
void reverse(char s[])
...{
int i, j;
int length;
char c;
i = 0;
while('/0' != s[i])
++i;
length = i;
for(i = length -2, j = 0; i >= length/2; --i, ++j) ...{
c = s[j];
s[j] = s[i];
s[i] = c;
}
}
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;
}
else if(c == EOF && i > 0)
...{
/**//**//**//* gotta do something about no newline preceding EOF */
s[i] = '/n';
++i;
}
s[i] = '/0';
return i;
}
本文介绍了一个使用C语言实现的字符串反转程序。通过定义getline函数读取一行输入,并使用reverse函数来实现字符串的反转。该程序展示了如何遍历字符串计算长度,并通过交换字符的方式完成反转过程。
2318

被折叠的 条评论
为什么被折叠?



