📌  相关文章
📜  在不使用任何内置函数的情况下将字符串转换为整数

📅  最后修改于: 2021-05-28 03:33:54             🧑  作者: Mango

给定字符串str ,任务是不使用任何内置函数将给定字符串转换为数字。

例子:

方法:想法是使用从48到57的0到9的数字的ASCII值。
因此,从字符的ASCII值的数字字符改变为整数减法器48将给出对于给定字符的相应数字。

下面是上述方法的实现:

C++
// C++ program for tha above approach 
#include  
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  
#include  
  
// 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

时间复杂度: O(N) ,其中N是给定字符串的长度。
辅助空间: O(1)