编程:利用指针将字符串反转

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void reverse(const char *ptr, char *str)
{
    int i, len = strlen(ptr);
    ptr = ptr + len - 1;
    for(i = 0; i < len; i++)
    {   
        *str = *ptr;
        str++;
        ptr--;
    }   
}

int main()
{
    char *ptr = "helloworld";
    char *str = (char *) malloc (sizeof(char) * 20);
    reverse(ptr, str);
    printf("%s\n",str);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_42720703/article/details/81210641