Trying to print reversed input in C:
#include <stdio.h>
#define MAX_SIZE 100
/* Reverses input */
void reverse(char string[], char reversed[]);
int main() {
char input[MAX_SIZE], output[MAX_SIZE + 1];
char c;
int index = 0;
while ((c = getchar()) != '\n')
{
input[index] = c;
++index;
}
reverse(input, output);
printf("%s\n", output);
}
void reverse(char string[], char reversed[]) {
int rev;
rev = 0;
for (int str = MAX_SIZE - 1; str >= 0; --str) {
if (string[str] != '\0') {
reversed[rev] = string[str];
++rev;
}
}
}
but have this weird result:
input:
abc
output:
?:? ????:???:?cba?
both input and output arrays comprise \0
, so I guess there's some index out of bounds exception, but I can't find out where exactly. Thank you.