# Convert String To Int

Difficulty Level Easy
Math StringViews 1323

Convert string to int, requires us to convert a numeric string given to an integer. Here we are assuming, we are provided with a numeric string without white spaces either in the middle of the characters or at any one of the ends. We are also assuming that the string provided to us doesn’t have any character other than the numbers from ‘0’ to ‘9’. Now, why do we even want to convert a string to an integer? If we have a numeric string then its harder to perform numeric arithmetical operations such as addition, subtraction, multiplication, etc. easily as compared to doing these operations on an integer.

But then you may ask, why do we even take a string as an input from the user? It would have been better if we took an integer as input from the user, right? Yes, it would have been better but that decreases our scalability. Because if a user mistakenly enters some characters other than numbers, our program will crash. So, it’s preferred to take input as a string and then afterward convert the string into an integer. After removal of characters, or white spaces if present. We have so many ways to get this conversion done from string to an integer, both in CPP and Java.  Now, we’ll be looking at some of the ways to convert a string to an integer.

## Example

`235`
`int n = 235`

Explanation: Here the “235” has been converted into an integer. `1ab`
`int n =  0   (as the string contains characters other than digits output is 0)`

## C++ Programs

### Using String Streams to convert string to int

#### Algorithm

```1. Initialise a string s of size n.
2. Initialise an object of the string stream class.
3. After that, create a variable of integer type and stream the object with the integer.
4. Print the integer variable.```

#### C++ Code

```#include <iostream>
#include <sstream>
using namespace std;

int main(){
string s = "235";

stringstream str(s);

int n = 0;
str>>n;

cout<<"int n = "<<n;

return 0;
}```
`int n = 235`

### Using sscanf() to convert string to int

#### Algorithm

```1. Initialise a const char str.
2. After that, initialise an object of the string stream class.
3. Create an variable of integer type and update the value of the integer variable using the sscanf.
4. Print the Integer variable.```

#### C++ Code

```#include <iostream>
#include <sstream>
using namespace std;

int main(){

const char *str = "235";
int n;
sscanf(str, "%d", &n);

cout<<"int n = "<<n;

return 0;
}```
`int n = 235`

### Using stoi() to convert string to int

stoi() takes string and returns it’s value.

#### Algorithm

```1. Initialise a string s of size n.
2. Create a variable of the integer type and update the value of the integer variable wih the function stoi().
3. Print the Integer variable.```

#### C++ code

```#include <iostream>
#include <string>
using namespace std;

int main(){

string s = "235";

int n = stoi(s);

cout<<"int n = "<<n;
return 0;
}```
`int n = 235`

### Using atoi() to convert string to int

atoi() takes the character array or the string and returns it’s value.

#### Algorithm

```1. Initialise an array of character type or a string literal s.
2. After that, create a variable of integer type and update the value of integer variable with the function atoi().
3. Print the Integer variable.```

#### C++ Code

```#include <iostream>
#include <string>
using namespace std;

int main(){

const char *s = "235";

int n = atoi(s);

cout<<"int n = "<<n;
return 0;
}```
`int n = 235`

## Java Programs to convert string to int

### Using Integer.parseInt()

#### Algorithm

```1. Initialise a string s of size n.
2. After that, create a variable of integer type and update the value of the integer variable as the parsed value of string to integer using the method Integer.parseInt(s).
3. Print the Integer variable.```

#### Java Code

```import java.io.*;

class convertStr{

public static int convert(String s){
int n = 0;
try{
n = Integer.parseInt(s);
}
catch(NumberFormatException e){
}

return n;
}

public static void main(String[] args){

String s = "235";
int n = convert(s);
System.out.println("int n = "+n);

}
}```
`int n = 235`

### Using Ints::tryParse method of Guava library

#### Algorithm

```1. Initialise a string s of size n and convert it into int using Ints::tryParse method.
2. Create a variable of the integer type and update the value of integer variable as the result.
3. Print the Integer variable.```

#### Java Code

```import java.io.*;
import java.util.*;

import java.io.*;

class convertStr{

public static int convert(String s){
int n = 0;

n = Optional.ofNullable(s).map(Ints::tryParse).orElse(0);

return n;
}

public static void main(String[] args){

String s = "235";
int n = convert(s);
System.out.println("int n = "+n);

}
}```
`int n = 235`
Translate »