c语言反转字符串输出

c语言反转字符串输出

c语言反转字符串输出,说难不难,说易不易!

#define _CRT_SECURE_NO_WARNINGS
#include 
#include 

char* rev_str(char *from,int len)
{
     
    //判断字符串是否存在
    if (from ==NULL )
    {
     
        printf("func copy_str21() err\n");
        return NULL;
    }

    //使指针p指向字符串尾部
    char* p = from + len - 1;
    //为了使len的值 一直保存的是字符串的长度
    int l = len;
    char* to = (char*)malloc(100);
    if (to == NULL)
    {
     
        printf("malloc err\n");
        return NULL;
    }

    //根据字符串长度,让字符串from最后一个字符换到字符串to的第一个字符以此类推
    for (;l>0;p--,l--,to++)
    {
     
        *to = *p;
    }
    //使字符串的最后一个字符是'\0'
    *to = '\0';
    //使to指针重新指向字符串的开头
    to = to - len;
    return to;
}

//字符串逆序
int main()
{
     
    char from[100] = "eeddccbbaa";
    printf("%s\n", to);//打印字符串
    int len = strlen(from);//求出字符串长度
    char* to = rev_str(from, len);
    printf("%s\n", to);//打印字符串
    free(to);
    system("pause");
}

你可能感兴趣的:(c语言,c语言,指针,字符串,printf,sizeof)