C語言strrev()函數:字串逆置(倒序、逆序)

來源:互聯網
上載者:User

標籤:檔案   崩潰   逆序   運行   函數   pre   include   修改   print   

標頭檔:#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

C語言strrev()函數:字串逆置(倒序、逆序)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.