## Having Trouble With a Recursion Problem that Converts a String to an Int

write a recursive function to convert a character string of digits to an integer
convert string to integer c++
int to string recursion
how to convert string number to integer in c++
c++ program to convert a string of number to integer
how to convert numeric string to an int
convert string to int in c
how to convert string array to integer array in c++

So the problem is as follows. You are to create a method that takes a string as a parameter that consists of all numbers and you need to take that string and convert it to an Integer. You have to use a recursive solution to solve the problem.

I have written the following code and it works just fine when I enter "1234" or "138775" but as soon as I enter a number that contains 0 it returns a weird result.

100, 1001, 101, and 12045 return 10, 11, 110, and 1245 respectively. As mentioned above The code works just fine when I send it things like "1234" or "14384" but as soon as there is a zero it has a tendency to remove that zero.

I have tried different int to string conversions from the Integer class such as parse(int) but that had the same result.

```/**
* converts a string of numbers to an int
*
* @param String str: original string of numbers
* @return int recursiveStringInt: returns the int value of the string
*/
public static int recursiveStringInt(String str)
{
if(str.length() == 1)
return Integer.valueOf(str);
else
{
return Integer.valueOf(str.substring(0,1) + recursiveStringInt(str.substring(1)));
}
}
```

Please let me know if there is any clarification needed.

Your parentheses are a bit off for what you're trying to do, and the logic needs some work.. what you want is to take the last character of the current string, convert it to an integer, and add it to the conversion of the front of the string times 10.

```int recursiveStringInt(String str) {
int length = str.length()
if(length == 1)
return Integer.valueOf(str);
else
{
int temp = Integer.valueOf(str.substring(length-1)) + ( 10 * recursiveStringInt(str.substring(0,length-1)));
return temp;
}
}
```

Trivial case of "8" results in just the first block being executed.

Next case of "83" results in temp = 3 + (10 * 8) = 83

Next case of "103" results in temp = 3 + (10 * (0 + (10 * 1))) = 103

The base case is an empty string. Each step of your recursion converts one character in the string to an integer. And the recursive step passes  Print reverse of a string using recursion; Minimum number of given operations required to convert a string to another string; Move all occurence of letter 'x' from the string s to the end using Recursion; Length of longest palindromic sub-string : Recursion; Convert string X to an anagram of string Y with minimum replacements

Try using a divide and power solution

```public static void main(String[] args) {

System.out.println(recursiveStringInt("12034", 0));

}

public static int recursiveStringInt(String str, int pow)
{
if(str.length() < 1)
return 0;
else
{
int temp = Integer.valueOf(str.substring(str.length() -1))
* (int) Math.pow(10.0, 1.0 * pow);
temp += recursiveStringInt(str.substring(0, str.length() -1), pow + 1);
return temp;
}
}
```

For example, convert the integer 10 to its string representation in decimal as "10" in the stack section, the recursive formulation of the problem is very elegant. Write a recursive C++ function for converting a string of digits into the integer it represents. For example, “13531” represents the integer 13531. Your function should return the integer value. I am able to convert the string to an integer but am having trouble on what the base case should be for the function to be recursive.

This is because when you parse a substring like `054` to int, it becomes `54`.

Try this code:-

```public static int recursiveStringInt(String str) {
return str.length() == 1
? Integer.valueOf(str)
: (int) (Integer.parseInt(str.substring(0, 1)) * Math.pow(10, str.length() - 1)) + recursiveStringInt(str.substring(1));
}
```

I have used this logic:-

`105 = 1*100 + 0*10 + 5*1`

Edit: If you don't understand the ternary operator, here is the if-else version:-

```public static int recursiveStringInt(String str) {
if (str.length() == 1) {
return Integer.valueOf(str);
} else {
return (int) (Integer.parseInt(str.substring(0, 1)) * Math.pow(10, str.length() - 1)) + recursiveStringInt(str.substring(1));
}
}
```

Problems. 8. Write a recursive algorithm that converts a string of numerals to an Write a recursive algorithm to check whether a specified character is in a string. 12. Write a recursive algorithm that changes an integer to a binary number. I am writing a recursive function to return an integer after converting a string. I am very close. This was already quite the brain teaser, and recursion is not my cup of tea. Perhaps somebody can help with this small little problem I am having. The last thing I need to do on this before I am

For example, convert the integer 10 to its string representation in decimal as "10" in the stack section, the recursive formulation of the problem is very elegant. After combing through dozens of common recursive problems, I’ve categorized all recursive problems into one of 6 categories. These 6 categories, based around the core pattern used to solve the problem, allow us to put a finite bound on the scope of recursive problems that we could need to solve.

Recursion is very important concept in computer science and one just cannot neglect We can say that recursion is “defining a problem in terms of itself” as it of a String in Java | Recursive & Iterative · Reverse given string using Recursion to n having sum n · Print all combinations of positive integers in increasing order  Move all occurence of letter 'x' from the string s to the end using Recursion; Count nodes in the given tree whose weight is a fibonacci number; Longest common subarray in the given two arrays; C/C++ program for calling main() in main() Count maximum occurrence of subsequence in string such that indices in subsequence is in A.P.

In the examples below, the solutions to Problems 1 and 2 are not tail Problem 1​, Reverse a string recursively: /* From the API: String substring(int For example, convert "aabccba" to "abcba": public String removeDuplicates(String word)  Adapt the ideas of printd to write recursive version of itoa; that is, convert integer into a string by calling a recursive routine Here is the implementation of printd: void printd(int n) { Stack Exchange Network

• Consider `System.out.println(Integer.valueOf("0123"));`