c語言代碼編程題匯總:把字串中的內容逆置

來源:互聯網
上載者:User

標籤:for   char   程式   c++   stdio.h   main   ring   div   內容   

字串中的內容逆置 

  程式碼如下:

 

 1 /* 2     2017年3月5日14:48:11 3     功能:把字串的內容逆置 4 */ 5  6 #include"string.h" 7 #include "stdio.h" 8 #define N 81 9 void fun(char *s)10 {11     int i, n = strlen(s)-1;                        //strlen()是求字串全部元素個數包括最後的‘\0‘(字串結尾的字元),而strlen()-1運算式所求得是數組最大下標值12     char t;13     for (i = 0; i < n; i++, n--)                //strlen()是求字串的長度,注意區分它不是求數組的長度,即char型數組,不適用int型數組。14     {15         t = s[i];16         s[i] = s[n];17         s[n] = t;18     }19 } 20 21 int main ()22 {23     char a[N];24     printf ("請輸入一個字串:\n");25     gets (a);                                    //當一次輸入一個字串時用gets()函數26     fun(a);27     printf ("修改後的字串是: \n");28     puts (a);                                    //當一次輸出一個字串時用puts()函數 29 30     return 0;31 }32 /*33     總結:34     1、getchar ()函數的用處是輸入單個字元35     2、scanf ()函數的用處是輸入整數型,浮點數型的資料36     3、在VC++6.0中顯示的結果:37     ————————————————————————38     請輸入一個字串:39     asdfghj40     修改後的字串是:41     jhgfdsa42 43     ————————————————————————44 */

 

c語言代碼編程題匯總:把字串中的內容逆置

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.