C语言strrev()函数:字符串逆置(倒序、逆序)

头文件:#include<string.h>

strrev()函数将字符串逆置,其原型为:
    char *strrev(char *str);

【参数说明】str为要逆置的字符串。

strrev()将str所指的字符串逆置。

【返回值】返回指向逆置后的字符串的指针。

strrev()不会生成新字符串,而是修改原有字符串。因此它只能逆置字符数组,而不能逆置字符串指针指向的字符串,因为字符串指针指向的是字符串常量,常量不能被修改。

【函数示例】看看strrev()是否改变原有字符串。

#include<stdio.h>
#include<string.h>
int main()
{
    // 若改为 char *str1 = "abcxyz";,程序在运行时会崩溃,为什么呢?
    char str1[] = "abcxyz";
    char *ret1 = strrev(str1);
    printf("The origin string of str1 is: %s
", str1);
    printf("The reverse string of str1 is: %s
", ret1);
    return 0;
}

运行结果:
The origin string of str1 is: abcxyz
The reverse string of str1 is: zyxcba

原文地址:https://www.cnblogs.com/guohaoyu110/p/6336760.html