# convert string into integer using recursion

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 08-21-2006
peter_hii
convert string into integer using recursion
hi, can someone give me an idea on how to convert string to integer using array
for example

string n = '123';
int c;

cout<<c;

and will display 123.

what i think of is 123 divide by 10 or the second idea tat i have is

for example: string s = 123

|1|2|3|
---------
0 1 2

and

s[n-1] = 3
+
s[n-2] = 2 * 10
+
s[n-3] = 1 * 100
=
123

Code:

```int stringConIntRec(string number) {         if(number.length() == 0)                 return 0;         else         {           ???????             ???????         } }```
• 08-21-2006
hk_mp5kpdw
One way to look at it:

0 * 10 = 0
0 + 1 = 1
1 * 10 = 10
10 + 2 = 12
12 * 10 = 120
120 + 3 = 123
• 08-21-2006
Tonto
Code:

`firstchar * 10 ^ depth + recursion(restofstring);`
depth can be tracked with length of string.
• 08-21-2006
anon
Are you required to do it that way?

Normally in C++ you'd use stringstream:

Code:

```#include <string> #include <sstream> int main() {     std::string s("123");     int i;     std::stringstream stoi(s);     stoi >> i;  //voila     return 0; }```
• 08-21-2006
peter_hii
hi Tonto, can u descrive what does '^' mean in your code please? sorry i am newbie of c++
• 08-21-2006
jinhao
there is a c++ way,
Code:

```template<typename _To, typename _From> _To lexcial_cast(_From from) {     _To to;     std::stringstream ss(from);     ss>>to;     return to; } int main() {     std::string str = "123"     int i = lexcial_cast<int>(str);     str = lexcial_cast<std::string>(i/10); }```
• 08-21-2006
peter_hii
this is my solution and seems like got some error... being looking and thinking but couldnt get it correct. can some one list the error..?

Code:

``` int convertRec(string num) {     int y;         if(num.length()==0)         return num;     else     {         int depth = num.length();         int position = 0;         int s = 4;                 y = (num[position] * 10 ^ depth) + convertRec(num.substr(depth-s, depth-10));         s--;     }     return y; }```
• 08-21-2006
Mario F.
Well, without looking at the actual recursion yet, I can tell you return num; will give you a compile error. You are trying to return a string from a function that returns an int.
• 08-21-2006
Daved
>> 10 ^ depth
Don't actually use ^. That doesn't mean to the power of in C++. Perhaps pow() is what you want.
• 08-21-2006
peter_hii
the power()? you mean power of deth?

Code:

``` int convertRec(string num) {     int y;         if(num.length()==0)         return 0;     else     {         int depth = num.length();         int position = 0;         int s = 4;                 y = pow(num[position] * 10, depth) + convertRec(num.substr(depth-s, depth-10));         s--;     }     return y; }```
is this wat you mea DAVED
• 08-21-2006
Mario F.
pow(num[position] * 10, depth)

You have to cast num[position] into an int.
Code:

`pow(static_cast<int>(num[position]) * 10, depth)`
• 08-21-2006
Mario F.
I'm not very good at looking at recursion. Always struggle with it. But I can tell you it will not work as it is.

The problem is that you are not creating a stop condition. Something has to make the recursion to stop. Probably the var depth.

You are not decremeting this value and my guess is that you should. With each call to the function you will want the base 10 exponent to decrease in order to produce the correct numbers 145 = 1*10^2 + 4*10^1 + 5*10^0.

Also, I'm not sure what s is doing there. Isn't it the same as the result of lenght()? If it is, you don't want it, do you?
• 08-21-2006
OnionKnight
On each iteration, multiply the current value by 10 and add the next.
Code:

```int stoi (string &s) {         int  num = 0;         int  i  = 0;         bool neg = false;         if (s[0] == '-') {                 neg = true;                 i++;         }         for (; i < s.length(); i++) {                 if (s[i] >= '0' && s[i] <= '9')                         num = num*10 + (s[i] - '0');                 else                         return -1;         }         if (neg)                 return -num;         return num; }```
• 08-23-2006
peter_hii
hi everyone, thank you for helping me but i really need to stick with recursion from converting string into integer.

really appreciate all effort....

hope can get some idea from others again...i am working on it as well...
• 08-23-2006
noyram08
first you must have a function which converts character 1 to 9 to integer 1 to 9;

try subtracting 48 to char but first you must store it to an integer variable

Code:

``` // assume you name this as num() function char s[n]; int x=char s[n]; int z=x-48; return z;```
then you can apply your idea:

[tag]
|1|2|3|
---------
0 1 2

and

s[n-1] = 3
+
s[n-2] = 2 * 10
+
s[n-3] = 1 * 100
=
123
[/tag]

you need to do a program which first start a the first element(assume s[0]) which keeps on incrementing until it hits the end (s[strlen(s)]), and about the "x10" problem you must do a power fuction like this:

Code:

``` int power(int n){ //n is the number nth of the element (i.e. s[1] here n=1) int x; for(x=0;x<n;x++){ int z=z*10; (can be z*=10) } return z;```
using this functions you can now create a program that answers your problem recursively. (^^ sorry can't make the program right now but hope this helps)
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last