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\n", str1);
printf("The reverse string of str1 is: %s\n", ret1);
return 0;
}
运⾏结果:
The origin string of str1 is: abcxyz
The reverse string of str1 is: zyxcba

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。