solutions/The C Programming Language/1.19.c

51 lines
753 B
C
Raw Permalink Normal View History

2024-05-01 04:40:57 -04:00
#include <stdio.h>
#define MAXLINE 10
int _getline(char line[], int maxline);
void reverse(char to[], char from[], int from_head);
// reversing input string
2024-05-01 04:40:57 -04:00
main()
{
int len;
char line[MAXLINE];
char reversed[MAXLINE];
while ((len = _getline(line, MAXLINE)) > 0)
{
reverse(reversed, line, len);
printf("%s\n", reversed);
}
return 0;
}
int _getline(char s[], int lim)
{
int c, i, j;
for (i = 0; (c = getchar()) != '\n'; ++i)
{
if (i < lim - 1)
s[i] = c;
}
j = i;
if (j > lim - 1)
j = lim - 1;
s[j] = '\0';
return j - 1;
}
void reverse(char to[], char from[], int from_head)
{
int i;
for (i = 0; i <= (from_head / 2); ++i)
{
to[i] = from[from_head - i];
to[from_head - i] = from[i];
}
to[from_head+1] = '\0';
}