2013-02-05 26 views
0

編寫一個從標準輸入 中讀取ASCII碼流並將字符發送到標準輸出的程序(過濾器)。該程序會丟棄除字母以外的其他所有字符 。任何小寫字母都以大寫字母形式輸出。 輸出由空格字符分隔的五個組中的字符。每10組輸出一個換行符 。 (行上的最後一組後面只有一個換行符; 行後的最後一組後面沒有空格。)最後一組的所有可能的 都少於五個字符,最後一行可能少一些比10組。假設輸入文件是任意長度的文本文件。爲此,使用getchar()和012charputchar()。你永遠不需要在內存中有一個以上的輸入數據字符 編寫密碼程序

我遇到麻煩的是如何做間距。我創建了一個包含5個對象的數組,但我不知道如何處理它。這是我到目前爲止:

#include <stdio.h> 
#include <stdlib.h> 
#include <ctype.h> 

int main() 
{ 
    char c=0, block[4]; 

    while (c != EOF) 
    { 
     c=getchar(); 

     if (isupper(c)) 
     { 
      putchar(c); 
     } 
     if (islower(c)) 
     { 
      putchar(c-32); 
     } 
    } 
} 
+1

聞起來像作業... –

回答

0
int main() 
{ 
    char c=0; 
    int charCounter = 0; 
    int groupCounter = 0; 

    while (c != EOF) 
    { 
     c=getchar(); 

     if (isupper(c)) 
     { 
      putchar(c); 
      charCounter++; 
     } 
     if (islower(c)) 
     { 
      putchar(c-32); 
      charCounter++; 
     } 

     // Output spaces and newlines as specified. 
     // Untested, I'm sure it will need some fine-tuning. 
     if (charCounter == 5) 
     { 
      putchar(' '); 
      charCounter = 0; 
      groupCounter++; 
     } 

     if (groupCounter == 10) 
     { 
      putchar('\n'); 
      groupCounter = 0; 
     } 
    } 
} 
+0

如果你給他答案,OP永遠不會學習如何創建算法。 –

+0

謝謝你! – user2023608

+1

如果你要感謝我,請UPVOTE我的答案,並接受它。 (http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) – abelenky

0

您不需要存儲字符,以執行您的問題中描述的算法。

您應該一次讀取一個字符,並跟蹤2個我不會透露的計數器。每個計數器將允許您知道在何處放置格式化輸出所需的特殊字符。

基本上是:

read a character 
if the character is valid for output then 
    convert it to uppercase if needed 
    output the character 
    update the counters 
    output space and or newlines according to the counters 
end if 

希望這會有所幫助。

另外:我不知道你試圖用block變量做的,但它被聲明爲4個元素的數組,無處文本是用4號...