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

itoa 與 atoi 的 C 語言手寫實現:面試必備

     編輯:關於C

itoa(整數轉字符串)與 atoi(字符串轉整數)是經典面試題,雖然標準庫已提供,但手寫實現能考察對進制轉換、字符處理、邊界條件的掌握程度。

atoi:字符串轉整數

核心邏輯

  1. 跳過前導空格(標準行為)
  2. 識別正負號
  3. 逐字符累加:result = result * 10 + (ch - '0')
  4. 遇非數字字符停止
  5. 應用正負號

實現代碼

int my_atoi(const char *str) {
    if (!str) return 0;

    // 跳過空格
    while (*str == ' ') str++;

    // 處理符號
    int sign = 1;
    if (*str == '-') { sign = -1; str++; }
    else if (*str == '+') { str++; }

    // 累加數值
    long long result = 0;  // 用長整型防溢出檢測
    while (*str >= '0' && *str <= '9') {
        result = result * 10 + (*str - '0');
        // 溢出檢測
        if (sign == 1 && result > INT_MAX) return INT_MAX;
        if (sign == -1 && -result < INT_MIN) return INT_MIN;
        str++;
    }

    return (int)(result * sign);
}

itoa:整數轉字符串

核心邏輯

  1. 處理 0 特例
  2. 處理負數:記錄符號,轉為正數處理
  3. 反向提取數字:digit = n % radix,n /= radix
  4. 數字映射字符:0-9 → '0'-'9',10-15 → 'A'-'F'
  5. 反轉字符串
  6. 添加負號

實現代碼(支援任意進制 2-36)

char *my_itoa(int n, char *str, int radix) {
    if (!str || radix < 2 || radix > 36) return NULL;

    static const char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    char *ptr = str;
    int sign = 0;

    // 處理 0
    if (n == 0) {
        *ptr++ = '0';
        *ptr = '\0';
        return str;
    }

    // 處理負數(僅十進制)
    if (n < 0 && radix == 10) {
        sign = 1;
        n = -n;
    }

    // 反向生成數字
    while (n != 0) {
        *ptr++ = digits[n % radix];
        n /= radix;
    }

    // 添加負號
    if (sign) *ptr++ = '-';

    *ptr = '\0';

    // 反轉字符串
    char *start = str, *end = ptr - 1;
    while (start < end) {
        char tmp = *start;
        *start++ = *end;
        *end-- = tmp;
    }

    return str;
}

測試用例

int main() {
    char buf[32];

    // atoi 測試
    printf("atoi: %d\n", my_atoi("12345"));      // 12345
    printf("atoi: %d\n", my_atoi("-678"));       // -678
    printf("atoi: %d\n", my_atoi("  +42"));      // 42
    printf("atoi: %d\n", my_atoi("12abc"));      // 12
    printf("atoi: %d\n", my_atoi("9999999999")); // INT_MAX (溢出保護)

    // itoa 測試
    printf("itoa: %s\n", my_itoa(12345, buf, 10));  // "12345"
    printf("itoa: %s\n", my_itoa(-123, buf, 10));   // "-123"
    printf("itoa: %s\n", my_itoa(255, buf, 16));    // "FF"
    printf("itoa: %s\n", my_itoa(10, buf, 2));      // "1010"
    printf("itoa: %s\n", my_itoa(0, buf, 10));      // "0"

    return 0;
}

關鍵點總結

函數易錯點應對策略
atoi溢出、空指針、前導空格、非數字字符用 long long 累加、檢查邊界、完善輸入驗證
itoaINT_MIN 取反溢出、進制映射、緩衝區大小用無符號型處理、查表法、調用者保證緩衝區

面試加分項

  • 提及 strtol/strtoul 等更健壯的標準庫函數
  • 討論線程安全性(標準 itoa 非線程安全,可改為輸出參數)
  • 擴展支援 long long、浮點數轉換
  • 說明為什麼標準庫不提供 itoa(非標準、緩衝區風險)

結語

手寫 atoi/itoa 不僅是面試題,更是理解「數值與文本表示互轉」本質的最佳練習。掌握邊界處理、進制轉換、字符編碼等細節,能讓你在系統編程中更從容地應對序列化、協議解析等場景。

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