Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to convert string to int without using library functions in c

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 7.67k
    Comment on it

    here we are converting string values to the integer without using library functions.

      #include
    
        int stringToInt(char[] );
        int main(){
    
            char str[10];
            int intValue;
    
            printf("Enter any integer as a string: ");
            scanf("%s",str);
    
           intValue = stringToInt(str); //converting string value to integer type by making its function.
    
            printf("Equivalent integer value: %d",intValue);
    
            return 0;
        }
    
        int stringToInt(char str[])       //it is a function made to convert the string value to integer value.
           {
            int i=0,sum=0;
    
            while(str[i]!='\0')                       //string not equals to null
             {
                 if(str[i]< 48 || str[i] > 57)             // ascii value of numbers are between 48 and 57.
                    {
                     printf("Unable to convert it into integer.\n");
                     return 0;
                 }
                 else
                {
                     sum = sum*10 + (str[i] - 48);
                     i++;
                 }
    
            }
    
            return sum;
    
        }
    
    Sample output:
    Enter any integer as a string: 456
    Equivalent integer value: 456
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: