頭文件:#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