Mickey Mathieson wrote:
> I am new to the group and have noticed a lot of questions of 
> recursion.
> A problem that is suitable for recursion should produce a smaller 
> function that if the problem was solved without recursion. This has
> not been the case from the postings that i have looked at. 
> 
> I have provided an example of recursion done correctly to reverse a 
> string. Notice how short and compact the code is. This is the object
> of recursion - less code at the expense of more memory being used.
> 
> Mickey M.
> senior software engineer
> http://www.constructionpartner.com
> 
> 
> // Reverse String
> char *RevStr(int count, char *str)
> {
>  char Value;
> 
>  if (count)
>  {
>               Value = str[count-1];
>               RevStr(count-1, str);
>       }
> 
>       if (count)
>               str[strlen(str) - count] = Value;
> 
>       return(str);
> }
> 
> int main(int argc, char* argv[])
> {
> 
>       char Test[20] = "ABCDEFGHIJK";
>       char Out[20];
> 
>       strcpy(Out,RevStr(strlen(Test), Test));
> 
> 
>       return 0;
> }

Far simpler, safer, and faster* to just use BString's Reverse() method:

BString MyStr("Test data");

printf("%s\n", *MyStr.Reverse());


* Calling strlen() every iteration of your code is a HUGE waste of CPU time.

If you really are a "Senior Software Engineer" as the title in your 
signature states, then you REALLY need to read Safe C++ Design 
Principles.  Recursion is a HUGE no-no.  I view the title of "Senior 
Software Engineer" as someone who is intimately familiar with the 
software within a company and is also the last line of defense to 
clearing changes to the product code base.  If you have the view that 
recursion is okay, then your code base is probably a disaster.

-- 
Thomas Hruska
CubicleSoft President
Ph: 517-803-4197

*NEW* VerifyMyPC 2.2
Change tracking and management tool.
Reduce tech. support times from 2 hours to 5 minutes.

Free for personal use, $10 otherwise.
http://www.CubicleSoft.com/VerifyMyPC/

Reply via email to