Hi there,
I have an issue on a simple program I'm trying to complete. The problem I have is on the "reverse" function, which probably has some error in its body. I'm quite beginner and so I probably missed something in char arrays. If you could help me with this program, I would really appreciate. Thanks in advance and please find here following the C code:
Code:
/* 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 MAXLENGTH 101 /* maximum input line length */
int getline(char line[], int limit);
void reverse(char d[], char s[], int len);
int main(void)
{
int len;
char rev[MAXLENGTH];
char line[MAXLENGTH];
while ((len = getline(line, MAXLENGTH)) > 0)
{
reverse(rev,line,len);
printf("\nLenght of this line is: %d\n",len);
printf("Original line = %s\nReverse line = %s",line, rev);
printf("\n\n");
}
return 0;
}
void reverse(char d[], char s[], int len)
{
int i,j;
j = len;
for (j=0; j<len; j++)
{
d[j]=0;
}
for (i=0; i<len; i++)
{
d[j] = s[i];
--j;
printf("s[%d]=%c\td[%d]=%c\n",i,s[i],j,d[j]);
}
return 0;
}
int getline(char line[], int limit)
{
int c,i;
for (i=0; i<=limit-1 && (c=getchar())!=EOF && c!='\n'; i++)
line[i]=c;
line[i]='\0';
return i;
}