在 C++ 中將數字字串轉換為整數


在此,我們將瞭解如何將數字字串轉換為整數型別資料。我們可以使用 atoi() 函式來解決此問題。此函式將字串作為輸入並轉換為整數資料。

atoi() 函式存在於 <<cstdlib> 庫中。

Input: A number string “1234”
Output: 1234

演算法

Step 1: Take a number string
Step 2: Convert it to integer using atoi() function
Step 3: Print the result.
Step 4: End

示例程式碼

 即時演示

#include<iostream>
#include<cstdlib>
using namespace std;
main() {
   int n;
   char num_string[20] = "1234";
   n = atoi(num_string);
   cout << n;
}

輸出

1234

更新於: 2019 年 7 月 30 日

252 次瀏覽

開啟你的 職業 生涯

完成課程即可獲得認證

開始學習
廣告
© . All rights reserved.