I have these three files main.h, 4-main.c and 4-print_rev.c and all three are required to print a string in reverse. I am not permitted to use standard libs, hence the reason for not using strlen() or printf() and I have to use _putchar() which performs same actions of putchar()
However, when I compile and run on my Linux sandbox, I get segmentation fault (core dumped) error.
I used an online compiler and it worked just as it should. Please help.
main.h
#define MAIN_H
#include <stdio.h>
#include <stdlib.h>
int _putchar(char c);
void reset_to_98(int *n);
void swap_int( int *a, int *b);
int _strlen(char *s);
void _puts(char *s);
void print_rev(char *s);
#endif
4-main.c
#include "main.h"
/**
* main - check the code
*
* Return: Always 0.
*/
int main(void)
{
char *str;
str = "I do not fear computers. I fear the lack of them - Isaac Asimov";
print_rev(str);
return (0);
}
4-print_rev.c
#include "main.h"
/**
* print_rev - prints a string
* @s: the string to be printed
*/
void print_rev(char *s)
{
int i;
while (s[i] != '\0')
{
i++;
}
while (i > 0)
{
_putchar(s[i-1]);
i--;
}
_putchar('\n');
}