Write a function reverse(s)
that reverses the character string s
. Use it to write a program that reverses its input a line at a time.
#include <stdio.h> #define MAX_LINE 1024 void discardnewline(char s[])
{
int i;
for(i = ; s[i] != '\0'; i++)
{
if(s[i] == '\n')
s[i] = '\0';
}
} int reverse(char s[])
{
char ch;
int i, j; for(j = ; s[j] != '\0'; j++)
{
} --j; for(i = ; i < j; i++)
{
ch = s[i];
s[i] = s[j];
s[j] = ch;
--j;
} return ;
} int getline(char s[], int lim)
{
int c, i; for(i = ; i < lim - && (c = getchar()) != EOF && c != '\n'; ++i)
{
s[i] = c;
} if(c == '\n')
{
s[i++] = c;
} s[i] = '\0'; return i; } int main(void)
{
char line[MAX_LINE]; while(getline(line, sizeof line) > )
{
discardnewline(line);
reverse(line);
printf("%s\n", line);
}
return ;
}