Convert number string to integers in C++

C++Server Side ProgrammingProgramming

Here we will see how to convert a number string to integer type data. We can solve this problem by using the atoi() function. This function takes string as input and converts into integer data.

The atoi() function is present in the <cstdlib> library.

Input: A number string “1234”
Output: 1234

Algorithm

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

Example Code

 Live Demo

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

Output

1234
raja
Published on 28-Mar-2019 08:37:08
Advertisements