My problem is in writing a function reverse(s)
that reverses the character string s
, a line at a time. My code here works, I write the line and it reverses the line. Is it a good solution?
#include <stdio.h>
#include <stdlib.h>
#define MAX 1000
void reverse(char cad[] ,char cadenita[],int i);
int main() {
int a ,i ;
char cad[MAX] = {0,0};
char cadenita[MAX];
i= 0;
while ((a = getchar()) != EOF) {
if (a != '\n') {
cad[i] = a;
++i;
}
else {
cad[i] = '\0' ;
reverse(cad , cadenita, i);
printf("%s\n",cadenita);
i = 0;
}
}
return (EXIT_SUCCESS);
}
void reverse(char s[] ,char svol[], int i) {
int a =0 ;
// i is the amount characters in s
while ((svol[a] = s[i-1]) != '\0' ){
++a;
--i;
}
}
Also in this part of code while ((svol[a] = s[i-1]) != '\0' ){
.
If I change to ((svol[a] = s[i-1]) != '\n' )
it still works and gives the same result, but why? I think there is an issue. Can you explain? Also, where does it break?