In: Computer Science
Explain your code with comments. Solve in C++.
1. Write a str2int() function that convers a string to integer. The
function will take the number as a string then will return it as
integer. E.g. str2int(“123”) will return 123 str2int(“005”) will
return 5 str2int(“102”) will return 102
Here is the solution of your question
here is the code
#include <iostream>
using namespace std;
//include string library to use string function
#include<string>
//function defination of str2int
int str2int(string s)
{ //stoi function is used to convert a string into int
return stoi(s);
}
int main()
{ //function calling
cout<<stoi("123")<<"\n";
cout<<stoi("005")<<"\n";
cout<<stoi("102")<<"\n";
return 0;
}