Given a string str, the task is to convert the given string into the number without using any inbuilt function.
Examples:
Input: str = “985632”
Output: 985632
Explanation:
Given input is in string form and returned output is in integer form.Input: str = “123”
Output: 123
Explanation:
Given input is in string form and returned output is in integer form.
Approach: The idea is to use the ASCII value of the digits from 0 to 9 start from 48 – 57.
Therefore, to change the numeric character to an integer subtract 48 from the ASCII value of the character will given the corresponding digit for the given character.
Below is the implementation of the above approach:
C++
// C++ program for tha above approach #include <iostream> using namespace std; // Function to convert string to // integer without using functions void convert(string s) { // Initialize a variable int num = 0; int n = s.length(); // Iterate till length of the string for (int i = 0; i < n; i++) // Subtract 48 from the current digit num = num * 10 + (int(s[i]) - 48); // Print the answer cout << num; } // Driver Code int main() { // Given string of number char s[] = "123"; // Function Call convert(s); return 0; } |
C
// C program for the above approach #include <stdio.h> #include <string.h> // Function to convert string to // integer without using functions void convert(char s[]) { // Initialize a variable int num = 0; int n = strlen(s); // Iterate till length of the string for (int i = 0; i < n; i++) // Subtract 48 from the current digit num = num * 10 + (s[i] - 48); // Print the answer printf("%d", num); } // Driver Code int main() { // Given string of number char s[] = "123"; // Function Call convert(s); return 0; } |
Java
// Java program for the above approach class GFG{ // Function to convert string to // integer without using functions public static void convert(String s) { // Initialize a variable int num = 0; int n = s.length(); // Iterate till length of the string for(int i = 0; i < n; i++) // Subtract 48 from the current digit num = num * 10 + ((int)s.charAt(i) - 48); // Print the answer System.out.print(num); } // Driver code public static void main(String[] args) { // Given string of number String s = "123"; // Function Call convert(s); } } // This code is contributed by divyeshrabadiya07 |
Python3
# Python3 program for the above approach # Function to convert string to # integer without using functions def convert(s): # Initialize a variable num = 0 n = len(s) # Iterate till length of the string for i in s: # Subtract 48 from the current digit num = num * 10 + (ord(i) - 48) # Print the answer print(num) # Driver code if __name__ == '__main__': # Given string of number s = "123" # Function Call convert(s) # This code is contributed by Shivam Singh |
C#
// C# program for the above approach using System; class GFG{ // Function to convert string to // integer without using functions public static void convert(string s) { // Initialize a variable int num = 0; int n = s.Length; // Iterate till length of the string for(int i = 0; i < n; i++) // Subtract 48 from the current digit num = num * 10 + ((int)s[i] - 48); // Print the answer Console.Write(num); } // Driver code public static void Main(string[] args) { // Given string of number string s = "123"; // Function call convert(s); } } // This code is contributed by rock_cool |
123
Time Complexity: O(N), where N is the length of the given string.
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Rearrange positive and negative numbers using inbuilt sort function
- Inbuilt function for calculating LCM in C++
- Check if any permutation of a number without any leading zeros is a power of 2 or not
- Efficiently check if a string has all unique characters without using any additional data structure
- Reverse string without using any temporary variable
- Efficiently find first repeated character in a string without using any additional data structure in one traversal
- Convert a String to an Integer using Recursion
- Digital Root (repeated digital sum) of square of an integer using Digital root of the given integer
- Find Quotient and Remainder of two integer without using division operators
- Generate a Binary String without any consecutive 0's and at most K consecutive 1's
- Smallest string without any multiplication sign that represents the product of two given numbers
- String matching with * (that matches with any) in any of the two strings
- To find sum of two numbers without using any operator
- To check a number is palindrome or not without using any extra space
- Print Number series without using any loop
- C Program to Print Number series without using any loop
- Largest of two distinct numbers without using any conditional statements or operators
- Count of numbers upto N digits formed using digits 0 to K-1 without any adjacent 0s
- Convert a String to Integer Array in C/C++
- C++ Program to Convert String to Integer
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

