String to Integer in Different Programming Languages (original) (raw)

Last Updated : 23 Jul, 2025

Below are example programs to do string to integer conversion in different programming languages.

C++ `

#include <bits/stdc++.h> using namespace std;

int main() { int val; char strn1[] = "12546";

val = atoi(strn1);
cout << "String value = " << strn1 << endl;
cout << "Integer value = " << val << endl;

char strn2[] = "GeeksforGeeks";
val = atoi(strn2);
cout << "String value = " << strn2 << endl;
cout << "Integer value = " << val << endl;

return (0);

}

// This code is contributed by shivanisinghss2110

C

#include <stdio.h> #include <stdlib.h> #include <string.h>

int main() { int val; char strn1[] = "12546";

val = atoi(strn1);
printf("String value = %s\n", strn1);
printf("Integer value = %d\n", val);

char strn2[] = "GeeksforGeeks";
val = atoi(strn2);
printf("String value = %s\n", strn2);
printf("Integer value = %d\n", val);

return (0);

}

Java

import java.util.*;

public class Main { public static void main(String[] args) { int val; String strn1 = "12546";

    val = Integer.parseInt(strn1);
    System.out.println("String value = " + strn1);
    System.out.println("Integer value = " + val);

    String strn2 = "GeeksforGeeks";
    try {
        val = Integer.parseInt(strn2);
        System.out.println("String value = " + strn2);
        System.out.println("Integer value = " + val);
    }
    catch (NumberFormatException e) {
        val = 0;
        System.out.println("String value = " + strn2);
        System.out.println("Integer value = " + val);
    }
}

}

Python

def main(): strn1 = "12546" val = int(strn1) print("String value = ", strn1) print("Integer value = ", val)

strn2 = "GeeksforGeeks"
try:
    val = int(strn2)
except ValueError:
    val = 0
    print("String value = ", strn2)
    print("Integer value = ", val)

if name == "main": main()

C#

using System;

namespace GeeksforGeeks { class Program { static void Main(string[] args) { String strn1 = "12546"; int val = int.Parse(strn1); Console.WriteLine("String value = " + strn1); Console.WriteLine("Integer value = " + val);

    String strn2 = "GeeksforGeeks";
    try {
        val = int.Parse(strn2);
    }
    catch (FormatException e) {
        val = 0;
        Console.WriteLine("String value = " + strn2);
        Console.WriteLine("Integer value = " + val);
    }
}

} }

JavaScript

// Javascript code to convert string to integer let val; let strn1 = "12546";

val = parseInt(strn1); console.log("String value = " + strn1); console.log("Integer value = " + val);

let strn2 = "GeeksforGeeks"; val = parseInt(strn2); console.log("String value = " + strn2); console.log("Integer value = " + val);

// This code is contributed by prasad264

`

Output

String value = 12546 Integer value = 12546 String value = GeeksforGeeks Integer value = 0

**Complexity Analysis of Standard Functions

Below is a typical time complexity analysis of the library functions

**Detailed Articles for Different Languages:

Below are detailed articles in different programming languages to do string to integer conversion.

String to Integer in C/C++ String to Integer in Java String to Integer in JavaScript String to Integer in Python String to Integer in C# String to Integer in PHP

How to implement your own conversion?

We need to write a function that takes a string as an argument and returns its equivalent integer value. Please refer the below post for details.

Write your own string to integer converter.