quick recursion question

This topic is 3850 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

so yeah, I'm usually good with doing recursion, but this particular java situation has got me puzzled. Basically what I'm trying to do is whatever string I'm given, i want to return a "cleaned" string where adjacent chars that are the same have been reduced to a single char. so basically "yyzzza" would be converted to "yza". the function i have constructed is: public String stringClean(String str){ } so basically, if there is adjacent multiples of any letter, its reduced to one letter in the string. Does anyone particularly have an idea what i should do?

Share on other sites
This sounds suspiciously like homework, which the forum has a policy against.

The problem is simple enough that we can't give legitimate hints without giving the complete answer.

Share on other sites
... smells like homework. Which is probably why you need (or think you need) to use recursion.

Share on other sites
i know you guys would think that buts not, im practing java on my own by doing the javabat's and this is one of the problems. i know the rules...

Share on other sites
Do you HAVE to use recursion? It seems like it would be much easier as a linear pass.

Share on other sites
Yeah it would be easier but part of the problem is to use recursion and no loops. Thats where I'm kinda stuck.

Share on other sites
Quote:
 Does anyone particularly have an idea what i should do?
Yep.

But it would help if you show what you've tried, and why your solutions didn't work.

Quote:
 Thats where I'm kinda stuck

One hint is to realize that the characters will only move backwards, or stay in same spot. So the new string will be of same size or smaller, and no character will ever move forward.

Share on other sites
Recursion can be (ab)used to simulate any(?) loop, so if you can write a loop without recursion that does this, it should be relatively straightforward to convert it into a recursive function.

For example, the infamous factorial function has a 1:1 conversion to a standard loop.

int FactorialLoop(int number){  int temp = 1;    while (number > 1)  {    temp *= number;    number = number - 1;  }  return temp;}int FactorialRecursion(int number){  if (number > 1)    return number * FactorialRecursion(number - 1);    return 1;}

Both cases have the same concepts:

1. A repeating section of code.
2. A "number" variable which is being decremented.
3. A temporary value being accumulated (the return value in the recursion).
4. A specific exit condition.

[Edited by - Nypyren on March 7, 2008 9:10:43 PM]

Share on other sites
Quote:
 Original post by AntheusOne hint is to realize that the characters will only move backwards, or stay in same spot. So the new string will be of same size or smaller, and no character will ever move forward.

I haven't used Java, but I've heard that C# is very similar to Java. Are strings in Java immutable like they are in C#?

If they are, then there won't be any "moving" of characters ;)

Share on other sites
never mind, i figured it out, but thanks for the help

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5
frob
13

• 11
• 9
• 30
• 16
• 9
• Forum Statistics

• Total Topics
632611
• Total Posts
3007417

×