#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int reverse_string(char *src_str, char *dst_str)
{
int length = 0;
int i = 0;
if(src_str == NULL || dst_str == NULL)
{
printf("the pointer is null\n");
return -1;
}
length = strlen(src_str);
if (length == 0)
{
printf("the string length is 0\n");
return 0;
}
memset(dst_str, 0, strlen(dst_str) + 1);
printf("[%s: %d] src_str is %s\n", __FUNCTION__, __LINE__, src_str);
//printf("[%s: %d] dst_str is %s\n", __FUNCTION__, __LINE__, dst_str);
for(i = 1; i <= length; i++)
{
memcpy(dst_str + i - 1, src_str + length - i, 1 );
}
*(dst_str + length) = '\0';
return 0;
}
int main(void)
{
char *mystr = "hello world!";
char *reverse_str = NULL;
reverse_str = (char *)malloc(strlen(mystr) + 1);
if(reverse_str == NULL)
{
printf("malloc failed\n");
return -1;
}
reverse_string(mystr, reverse_str);
printf("reverse_str is %s\n", reverse_str);
free(reverse_str);
return 0;
}