《C程式設計語言》練習

來源:互聯網
上載者:User

標籤:編寫   程式   color   長度   getc   div   輸入   條件   pre   

練習1-9:編寫一個將輸入複製到輸出的程式,並將其中連續的多個空格用一個空格代替。

  思路是用一個遊標state標記,如果遇到一個空格就將他變成0,直到變成非空格,將state變成1,先判斷條件,如果state是0則不輸出。

  代碼:

while(c=getchar() != EOF){    if(c == ‘ ‘ && state == 0)        ;    else if(c == ‘ ‘){        putchar(c);        state=0;    }    else{        putchar(c);        state=1;    }}

 

1.14:編寫一個程式,列印輸入中各個字元出現頻度的長條圖。

  水平方向:

#include<stdio.h>main(){    char c;    int i,a[128];    for(i=0;i<128;i++)        a[i] = 0;        while(scanf("%c",&c) != EOF){        a[c]++;    }    for(i=0;i<128;i++){        printf("%-4d",i);        while(a[i] != 0){            printf("-");            a[i]--;        }        printf("\n");    }}

 

練習1-16:修改列印最長文本行的程式的主程式main,使之可以列印任意長度的輸入行的長度,並儘可能多地列印文本。

    

《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.