C++ program to "Convert String to it's corresponding Integer" using Recursion

In this program we'll learn How to convert a string into into corresponding integer value.
Example:
To convert string "1234" to its integer value 1234.

#include <iostream>
#include<string.h>
#include<math.h>


using namespace std;
int StrToInt(char S[],int j,int i,int a,int b)
{
    if(S[i]=='\0')
    {
        return b;
    }

        a=S[i]-'0';
        b=a*(pow(10,j))+StrToInt(S,j-1,i+1,a,b);

    return b;
}

int main()
{
    char S[50];
    cin>>S;
    int j=strlen(S);
    int y=StrToInt(S,j-1,0,0,0);
    cout<<y;


    return 0;
}

Comments

Popular posts from this blog