I wrote a function to reverse a char-array (string).
Since I'm beginner and didn't work with malloc and stuff before, maybe someone could take a look, if this is fine, what I'm doing here?
char* reverse_string(char* string)
{
// getting actual length of the string
size_t length = strlen(string);
// allocate some space for the new string
char* new_string = malloc(sizeof(char)*(length+1));
// index for looping over the string
int actual_index = 0;
// iterating over the string until '\0'
while(string[actual_index] != '\0')
new_string[length-actual_index-1] = string[actual_index++];
// setting the last element of string-array to '\0'
new_string[length] = '\0';
// free up the allocated memory
free(new_string);
// return the new string
return new_string;
}