This commit is contained in:
NaiJi ✨ 2024-05-01 12:40:57 +04:00
parent 9cea189229
commit 1c56e1c7e3
1 changed files with 48 additions and 0 deletions

View File

@ -0,0 +1,48 @@
#include <stdio.h>
#define MAXLINE 10
int _getline(char line[], int maxline);
void reverse(char to[], char from[], int from_head);
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';
}