Posted on by Kalkicode
Code Conversion

Program to convert string to integer

Converting a string to an integer means converting a string that contains a numeric value into an integer data type. For example, converting the string "12345" to the integer 12345.

Here given code implementation process.

``````/*
C program for
Program to convert string to integer
*/
#include <stdio.h>
#include <stdlib.h> // for atoi
#include <string.h> // strlen

void stringToInteger(char num[])
{
// Get length of num
int n = strlen(num);
printf("\n Given number : %s", num);
// Case A
// Use atoi function
printf("\n Using atoi   : %d", atoi(num));
// Case B
// Custom code
int result = 0;
int i = 0;
if (num[0] == '-')
{
// When num is negative number
i = 1;
}
// This loop execute i..n
// And combine digits to make integer
while (i < n)
{
result = (result *10) + (num[i] - '0');
i++;
}
if (num[0] == '-')
{
// When num is negative number
result = -result;
}
printf("\n Result    : %d\n", result);
}
int main()
{
// Given string numbers
char num1[] = "12345";
char num2[] = "-12345";
char num3[] = "8000";
// Test Case
stringToInteger(num1);
stringToInteger(num2);
stringToInteger(num3);
return 0;
}``````

Output

`````` Given number : 12345
Using atoi   : 12345
Result    : 12345

Given number : -12345
Using atoi   : -12345
Result    : -12345

Given number : 8000
Using atoi   : 8000
Result    : 8000``````
``````/*
Java Program for
Program to convert string to integer
*/
public class Conversion
{
public void stringToInteger(String num)
{
// Get length of given number
int n = num.length();
System.out.print("\n Given number : " + num);
// Case A
// Use parseInt function
// Assume that given number is valid number
int result1 = Integer.parseInt(num);
// Display result1
System.out.print("\n Using parseInt : " + result1);
// Case B
// Use valueOf function
// Assume that given number is valid number
int result3 = Integer.valueOf(num);
// Display result3
System.out.print("\n Using valueOf : " + result3);
// Case C
// Custom code
int result2 = 0;
int i = 0;
if (num.charAt(0) == '-')
{
// When num is negative number
i = 1;
}
// This loop execute i..n
// And combine digits to make integer
while (i < n)
{
result2 = (result2 * 10) + (num.charAt(i) - '0');
i++;
}
if (num.charAt(0) == '-')
{
// When num is negative number
result2 = -result2;
}
// Display result2
System.out.println("\n Result : " + result2);
}
public static void main(String[] args)
{
// Given string numbers
String num1 = "12345";
String num2 = "-12345";
String num3 = "8000";
// Test Case
}
}``````

Output

`````` Given number : 12345
Using parseInt : 12345
Using valueOf : 12345
Result : 12345

Given number : -12345
Using parseInt : -12345
Using valueOf : -12345
Result : -12345

Given number : 8000
Using parseInt : 8000
Using valueOf : 8000
Result : 8000``````
``````// Include header file
#include <iostream>

#include <string>

using namespace std;
/*
C++ Program for
Program to convert string to integer
*/
class Conversion
{
public: void stringToInteger(string num)
{
// Get length of given number
int n = num.length();
cout << "\n Given number : " << num;
// Case A
// Use stoi function
// Assume that given number is valid number
int result1 = stoi(num);
// Display result3
cout << "\n Using stoi : " << result1;
// Case C
// Custom code
int result2 = 0;
int i = 0;
if (num[0] == '-')
{
// When num is negative number
i = 1;
}
// This loop execute i..n
// And combine digits to make integer
while (i < n)
{
result2 = (result2 *10) + (num[i] - '0');
i++;
}
if (num[0] == '-')
{
// When num is negative number
result2 = -result2;
}
// Display result2
cout << "\n Result : " << result2 << endl;
}
};
int main()
{
// Given string numbers
string num1 = "12345";
string num2 = "-12345";
string num3 = "8000";
// Test Case
return 0;
}``````

Output

`````` Given number : 12345
Using stoi : 12345
Result : 12345

Given number : -12345
Using stoi : -12345
Result : -12345

Given number : 8000
Using stoi : 8000
Result : 8000``````
``````package main
import "strconv"
import "fmt"
/*
Go Program for
Program to convert string to integer
*/

func stringToInteger(num string) {
// Get length of given number
var n int = len(num)
fmt.Print("\n Given number : ", num)
// Case A
// Use strconv.Atoi function
// Assume that given number is valid number
var result1 , err = strconv.Atoi(num)

if err != nil {
return
}
// Display result1
fmt.Print("\n Using strconv.Atoi : ", result1)

// Case B
// Custom code
var result2 int = 0
var i int = 0
if num[0] == '-' {
// When num is negative number
i = 1
}
// This loop execute i..n
// And combine digits to make integer
for (i < n) {
result2 = (result2 * 10) + int(num[i] - '0')
i++
}
if num[0] == '-' {
// When num is negative number
result2 = -result2
}
// Display result2
fmt.Println("\n Result : ", result2)
}
func main() {

// Given string numbers
var num1 string = "12345"
var num2 string = "-12345"
var num3 string = "8000"
// Test Case
stringToInteger(num1)
stringToInteger(num2)
stringToInteger(num3)
}``````

Output

`````` Given number : 12345
Using strconv.Atoi : 12345
Result :  12345

Given number : -12345
Using strconv.Atoi : -12345
Result :  -12345

Given number : 8000
Using strconv.Atoi : 8000
Result :  8000
``````
``````// Include namespace system
using System;
/*
Csharp Program for
Program to convert string to integer
*/
public class Conversion
{
public void stringToInteger(String num)
{
// Get length of given number
int n = num.Length;
Console.Write("\n Given number : " + num);
// Case A
// Use Int32.Parse function
// Assume that given number is valid number
int result1 = Int32.Parse(num);
// Display result1
Console.Write("\n Using Int32.Parse : " + result1);
// Case B
// Use TryParse function
// Assume that given number is valid number
int result3 = 0;
Int32.TryParse(num,out result3);
// Display result3
Console.Write("\n Using TryParse : " + result3);
// Case C
// Custom code
int result2 = 0;
int i = 0;
if (num[0] == '-')
{
// When num is negative number
i = 1;
}
// This loop execute i..n
// And combine digits to make integer
while (i < n)
{
result2 = (result2 * 10) + (num[i] - '0');
i++;
}
if (num[0] == '-')
{
// When num is negative number
result2 = -result2;
}
// Display result2
Console.WriteLine("\n Result : " + result2);
}
public static void Main(String[] args)
{
// Given string numbers
String num1 = "12345";
String num2 = "-12345";
String num3 = "8000";
// Test Case
}
}``````

Output

`````` Given number : 12345
Using Int32.Parse : 12345
Using TryParse : 12345
Result : 12345

Given number : -12345
Using Int32.Parse : -12345
Using TryParse : -12345
Result : -12345

Given number : 8000
Using Int32.Parse : 8000
Using TryParse : 8000
Result : 8000``````
``````<?php
/*
Php Program for
Program to convert string to integer
*/
class Conversion
{
public	function stringToInteger(\$num)
{
// Get length of given number
\$n = strlen(\$num);
echo("\n Given number : ".\$num);
// Case A
// Use number_format function
// Assume that given number is valid number
\$result1 = number_format(\$num);
// Display result1
echo("\n Using number_format : ".\$result1);
// Case B
// Custom code
\$result2 = 0;
\$i = 0;
if (\$num[0] == '-')
{
// When num is negative number
\$i = 1;
}
// This loop execute i..n
// And combine digits to make integer
while (\$i < \$n)
{
\$result2 = (\$result2 * 10) + (ord(\$num[\$i]) - ord('0'));
\$i++;
}
if (\$num[0] == '-')
{
// When num is negative number
\$result2 = -\$result2;
}
// Display result2
echo("\n Result : ".\$result2."\n");
}
}

function main()
{
// Given string numbers
\$num1 = "12345";
\$num2 = "-12345";
\$num3 = "8000";
// Test Case
}
main();``````

Output

`````` Given number : 12345
Using number_format : 12,345
Result : 12345

Given number : -12345
Using number_format : -12,345
Result : -12345

Given number : 8000
Using number_format : 8,000
Result : 8000``````
``````/*
Node JS Program for
Program to convert string to integer
*/
class Conversion
{
stringToInteger(num)
{
// Get length of given number
var n = num.length;
process.stdout.write("\n Given number : " + num);
// Case A
// Use parseInt function
// Assume that given number is valid number
var result1 = parseInt(num);
// Display result1
process.stdout.write("\n Using parseInt : " + result1);
// Case B
// Use Number function
// Assume that given number is valid number
var result3 = Number(num);
// Display result3
process.stdout.write("\n Using Number : " + result3);
// Case C
// Custom code
var result2 = 0;
var i = 0;
if (num.charAt(0) == '-')
{
// When num is negative number
i = 1;
}
// This loop execute i..n
// And combine digits to make integer
while (i < n)
{
result2 = (result2 * 10) +
(num.charAt(i).charCodeAt(0) - '0'.charCodeAt(0));
i++;
}
if (num.charAt(0) == '-')
{
// When num is negative number
result2 = -result2;
}
// Display result2
console.log("\n Result : " + result2);
}
}

function main()
{
// Given string numbers
var num1 = "12345";
var num2 = "-12345";
var num3 = "8000";
// Test Case
}
main();``````

Output

`````` Given number : 12345
Using parseInt : 12345
Using Number : 12345
Result : 12345

Given number : -12345
Using parseInt : -12345
Using Number : -12345
Result : -12345

Given number : 8000
Using parseInt : 8000
Using Number : 8000
Result : 8000``````
``````#    Python 3 Program for
#    Program to convert string to integer
class Conversion :
def stringToInteger(self, num) :
#  Get length of given number
n = len(num)
print("\n Given number : ", num, end = "")
#  Case A
#  Use int function
#  Assume that given number is valid number
result1 = int(num)
#  Display result3
print("\n Using int : ", result1, end = "")
#  Case B
#  Custom code
result2 = 0
i = 0
if (num[0] == '-') :
#  When num is negative number
i = 1

#  This loop execute i..n
#  And combine digits to make integer
while (i < n) :
result2 = (result2 * 10) + (ord(num[i]) - ord('0'))
i += 1

if (num[0] == '-') :
#  When num is negative number
result2 = -result2

#  Display result2
print("\n Result : ", result2)

def main() :
#  Given string numbers
num1 = "12345"
num2 = "-12345"
num3 = "8000"
#  Test Case

if __name__ == "__main__": main()``````

Output

`````` Given number :  12345
Using int :  12345
Result :  12345

Given number :  -12345
Using int :  -12345
Result :  -12345

Given number :  8000
Using int :  8000
Result :  8000``````
``````#    Ruby Program for
#    Program to convert string to integer
class Conversion
def stringToInteger(num)
#  Get length of given number
n = num.length
print("\n Given number : ", num)
#  Case A
#  Use Integer function
#  Assume that given number is valid number
result1 = Integer(num)
#  Display result1
print("\n Using Integer : ", result1)
#  Case B
#  Use to_i
result3 = num.to_i
#  Display result3
print("\n Using to_i : ", result3)
#  Case C
#  Custom code
result2 = 0
i = 0
if (num[0] == '-')
#  When num is negative number
i = 1
end

#  This loop execute i..n
#  And combine digits to make integer
while (i < n)
result2 = (result2 * 10) + (num[i].ord - '0'.ord)
i += 1
end

if (num[0] == '-')
#  When num is negative number
result2 = -result2
end

#  Display result2
print("\n Result : ", result2, "\n")
end

end

def main()
#  Given string numbers
num1 = "12345"
num2 = "-12345"
num3 = "8000"
#  Test Case
end

main()``````

Output

`````` Given number : 12345
Using Integer : 12345
Using to_i : 12345
Result : 12345

Given number : -12345
Using Integer : -12345
Using to_i : -12345
Result : -12345

Given number : 8000
Using Integer : 8000
Using to_i : 8000
Result : 8000
``````
``````/*
Scala Program for
Program to convert string to integer
*/
class Conversion()
{
def stringToInteger(num: String): Unit = {
// Get length of given number
var n: Int = num.length();
print("\n Given number : " + num);
// Case A
// Use parseInt function
// Assume that given number is valid number
var result1: Int = Integer.parseInt(num);
// Display result1
print("\n Using parseInt : " + result1);
// Case B
// Use valueOf function
// Assume that given number is valid number
var result3: Int = num.toInt;
// Display result3
print("\n Using valueOf : " + result3);
// Case C
// Custom code
var result2: Int = 0;
var i: Int = 0;
if (num.charAt(0) == '-')
{
// When num is negative number
i = 1;
}
// This loop execute i..n
// And combine digits to make integer
while (i < n)
{
result2 = (result2 * 10) + (num.charAt(i).toInt - '0'.toInt);
i += 1;
}
if (num.charAt(0) == '-')
{
// When num is negative number
result2 = -result2;
}
// Display result2
println("\n Result : " + result2);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Conversion = new Conversion();
// Given string numbers
var num1: String = "12345";
var num2: String = "-12345";
var num3: String = "8000";
// Test Case
}
}``````

Output

`````` Given number : 12345
Using parseInt : 12345
Using valueOf : 12345
Result : 12345

Given number : -12345
Using parseInt : -12345
Using valueOf : -12345
Result : -12345

Given number : 8000
Using parseInt : 8000
Using valueOf : 8000
Result : 8000``````
``````/*
Swift 4 Program for
Program to convert string to integer
*/
class Conversion
{
func stringToInteger(_ data: String)
{
let num = Array(data);
// Get length of given number
let n: Int = num.count;
print("\n Given number : ", data, terminator: "");

// Case A
// Use valueOf function
// Assume that given number is valid number
let result1: Int = Int(data)!;
// Display result1
print("\n Using valueOf : ", result1, terminator: "");
// Case B
// Custom code
var result2: Int = 0;
var i: Int = 0;
if (num[0] == "-")
{
// When num is negative number
i = 1;
}
// This loop execute i..n
// And combine digits to make integer
while (i < n)
{
result2 = (result2 * 10) +
(Int(UnicodeScalar(String(num[i]))!.value) -
Int(UnicodeScalar(String("0"))!.value));
i += 1;
}
if (num[0] == "-")
{
// When num is negative number
result2 = -result2;
}
// Display result2
print("\n Result : ", result2);
}
}
func main()
{
// Given string numbers
let num1: String = "12345";
let num2: String = "-12345";
let num3: String = "8000";
// Test Case
}
main();``````

Output

`````` Given number :  12345
Using valueOf :  12345
Result :  12345

Given number :  -12345
Using valueOf :  -12345
Result :  -12345

Given number :  8000
Using valueOf :  8000
Result :  8000``````
``````/*
Kotlin Program for
Program to convert string to integer
*/
class Conversion
{
fun stringToInteger(num: String): Unit
{
// Get length of given number
val n: Int = num.length;
print("\n Given number : " + num);
// Case A
// Use toInt function
// Assume that given number is valid number
val result1: Int = num.toInt();
// Display result1
print("\n Using parseInt : " + result1);
// Case B
// Use toIntOrNull function
// Assume that given number is valid number
val result3 = num.toIntOrNull();
// Display result3
print("\n Using toIntOrNull : " + result3);
// Case C
// Custom code
var result2: Int = 0;
var i: Int = 0;
if (num.get(0) == '-')
{
// When num is negative number
i = 1;
}
// This loop execute i..n
// And combine digits to make integer
while (i < n)
{
result2 = (result2 * 10) +
(num.get(i).toInt() - '0'.toInt());
i += 1;
}
if (num.get(0) == '-')
{
// When num is negative number
result2 = -result2;
}
// Display result2
println("\n Result : " + result2);
}
}
fun main(args: Array < String > ): Unit
{
// Given string numbers
val num1: String = "12345";
val num2: String = "-12345";
val num3: String = "8000";
// Test Case
}``````

Output

`````` Given number : 12345
Using parseInt : 12345
Using toIntOrNull : 12345
Result : 12345

Given number : -12345
Using parseInt : -12345
Using toIntOrNull : -12345
Result : -12345

Given number : 8000
Using parseInt : 8000
Using toIntOrNull : 8000
Result : 8000``````

Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post