I am currently learning C and trying to do the problems from The C Programming Language, not sure why this error is occurring. Question:
Exercise 1-19. 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.
I have tried to comment out the whole segment of the function reverse(make it do nothing), and yet it doesn't help. getln was provided by the textbook.
#include <stdio.h>
#define MAXLINE 1000
int getln(char line[], int limit);
void reverse (char line[]);
main(){
int len;
char line[MAXLINE];
while ((len = getln(line, MAXLINE)) > 0){
reverse(line);
printf('%s',line);
}
}
/*returns length of s*/
int getln(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;
}
s[i] = '\0';
return i;
}
void reverse(char s[]){
int len, i;
len = sizeof(s)/sizeof(s[0]);
char temp[len];
for (i = 0; i < len; ++i){
temp[i] = s[len - i];
}
while ((s[i] = temp[i] != '\0'))
++i;
}
I was expecting the line that I entered to be reversed. Yet, error shows - Segmentation fault.
Thank you!