首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

C語言字元統計程式:標準輸入多行文字分類計數實作

     編輯:關於C語言

字元統計是文字處理的基礎功能,本文實作一個從標準輸入讀取多行文字,分別統計英文字母、數字、空白字元與其他字元數量的 C 程式,並深入解析輸入處理與字元分類技巧。

完整程式碼實作

#include <stdio.h>
#include <string.h>

int main() {
    long letters = 0, digits = 0, spaces = 0, others = 0;
    char line[100000];
    size_t len;
    int ch;

    while ((ch = getchar()) != EOF) {
        // 處理換行符:歸類為其他字元或單獨統計
        if (ch == '\n') {
            others++;  // 或單獨設行數計數器
            continue;
        }

        if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
            letters++;
        } else if (ch >= '0' && ch <= '9') {
            digits++;
        } else if (ch == ' ') {
            spaces++;
        } else {
            others++;
        }
    }

    printf("字母: %ld  數字: %ld  空白: %ld  其他: %ld\n", 
           letters, digits, spaces, others);
    return 0;
}

關鍵技術細節

逐字元讀取 vs 整行讀取

方式函數優點缺點
逐字元getchar()記憶體恆定、即時處理、無緩衝區限制函數呼叫開銷大
整行fgets()/gets()批次處理效率高需大緩衝區、gets() 不安全

本程式採用 getchar() 逐字元處理,避免 gets() 緩衝區溢位風險 (已從 C11 標準移除)。

字元分類標準庫替代方案

使用 <ctype.h> 可移植分類函數:

#include <ctype.h>

if (isalpha(ch)) letters++;
else if (isdigit(ch)) digits++;
else if (isspace(ch)) spaces++;  // 包含空白、Tab、換行等
else others++;

優點:自動處理地區設定、支援非 ASCII 字元集。缺點:函數呼叫開銷、行為受 setlocale 影響。

EOF 處理與跨平台差異

  • Windows:Ctrl+Z 送出 EOF
  • Linux/macOS:Ctrl+D 送出 EOF
  • 檔案重導向:./stat < input.txt 自動以檔案結尾為 EOF

進階功能擴充

統計單字數

int in_word = 0;
long words = 0;

while ((ch = getchar()) != EOF) {
    if (isspace(ch)) {
        in_word = 0;
    } else if (!in_word) {
        in_word = 1;
        words++;
    }
    // 原有分類邏輯...
}

統計行數與最長行

long lines = 0, max_len = 0, cur_len = 0;
while ((ch = getchar()) != EOF) {
    if (ch == '\n') {
        lines++;
        if (cur_len > max_len) max_len = cur_len;
        cur_len = 0;
    } else {
        cur_len++;
    }
}

完整 wc 工具模擬

// 模擬 Linux wc 命令:行數、單字數、位元組數
long lines=0, words=0, bytes=0;
int in_word=0, ch;
while ((ch=getchar())!=EOF) {
    bytes++;
    if (ch=='\n') lines++;
    if (isspace(ch)) in_word=0;
    else if (!in_word) { in_word=1; words++; }
}
printf("%ld %ld %ld\n", lines, words, bytes);
字元統計程式執行示意圖 多行輸入統計結果

常見陷阱與最佳實踐

  1. 避免使用 gets():無緩衝區大小檢查,C11 已移除,改用 fgets(buf, sizeof(buf), stdin)
  2. signed char 比較陷阱:char 有無號實作相關,字元分類建議轉 unsigned char 或用 int 接收 getchar() 回傳值
  3. 換行符分類:決定歸類為「空白」或「其他」需依需求文件定義
  4. 大檔案效能:大量資料考慮 fread() 區塊讀取 + 手動解析

效能優化建議

  • 使用查表法:static char type[256] 預計算字元類型,O(1) 分類無分支預測失誤
  • SIMD 向量化:處理 16/32/64 位元組並行 (需平台相關內建函數)
  • 多執行緒:檔案分段並行統計,最後彙總

字元統計雖屬基礎練習,但涵蓋輸入流處理、字元編碼認知、標準庫應用、效能優化等核心議題。從簡單計數器延伸至 wc 工具、文字分析引擎,是系統程式設計的重要基石。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved