As practice with the (ever so painful) C Strings I wrote a string reversal program. I am new to C, so I wanted to make sure that I am not using bad coding practices. It runs just fine (at least with expected input), but I am not sure if the logic is as good as it could be. (Note: I do have #include to stdio, stdlib, and string)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* strreverse(char* c)
{
char input[500], output[500] = {}, buffer[2] = {' ', '\0'};
strcpy(input, c);
int i;
for(i = strlen(input); i >= 0; i--)
{
buffer[0] = input[i];
strcat(output, buffer);
}
return (char*) output;
}
int main(void)
{
char* in = malloc(256);
printf("Enter a string: ");
scanf("%s", in);
char* out = strreverse(in);
printf("%s", out);
return 0;
}