Programming Challenge!

AceInfinity

Emeritus, Contributor
Joined
Feb 21, 2012
Posts
1,728
Location
Canada
Here's a challenge for you guys !

Objective:
Write a function that returns the length of a string variable.

The Catch/Guidelines:
- Write a recursive function that only takes one parameter (of type string). Example:
Code:
public static int GetStrLen(string input)
{
	// Get the length of the string: input
	// Return the value for the length of this string
	// Keep in mind the rules...
{
- This cannot be an iterative function, it must be recursive
- You cannot use any counter variable to keep track
- You cannot use any of the properties available to the string class or type (depending on what language you're using). (Ex: No use of the Length property for strings in .NET, no len() in Python, etc...)

I completed this in C#, and Python so far, lets see who else can do it!

:thumbsup2:
 
It is a good challenge, thank you.

Are we allowed to use any other variables at all?

I can figure out the majority of the approach, but I have yet to work out how to make it either work on every string (I can quite easily make it work by making it break for special case strings), or without an extra, non-counter variable (which is also easy). Now just to figure out a way without an extra variable and make it work with all strings. Hmmmmm.
 
One C++ (untested pseudo-code) solution (using another variable):

Read More:
 
It is a good challenge, thank you.

Are we allowed to use any other variables at all?

I can figure out the majority of the approach, but I have yet to work out how to make it either work on every string (I can quite easily make it work by making it break for special case strings), or without an extra, non-counter variable (which is also easy). Now just to figure out a way without an extra variable and make it work with all strings. Hmmmmm.

You can use other variables if you want, but only local variables, no external member variables for counting.

One C++ (untested pseudo-code) solution (using another variable):

Read More:

There's some issues in that code there, but you might be on the right track... :)

C# I did it this way:
Read More:

In Python I did it this way:
Read More:

Which can also be done this way:
Read More:
 
Last edited:
C++ solution that I came up with:
Read More:
 
Last edited:
Thank you for the clarification. Firstly, I realise I was being an idiot with my static variable :p I can easily do away with a whole if statement.

I will continue to think.

Richard
 
Code:
int func1(char cArray[])
{
    DWORD reg32_1, reg32_2;
    BYTE reg8_1;

    reg32_1 = (DWORD)cArray;
    reg32_2 = (DWORD)reg32_1 + 1;

    label1:
    reg8_1 = *(BYTE*)reg32_1;
    reg32_1++;

    if (reg8_1 != 0)
    {
        goto label1;
    }
    else
    {
        reg32_1 -= reg32_2;
        return reg32_1;
    }
}
lel
 
Code:
int func1(char cArray[])
{
    DWORD reg32_1, reg32_2;
    BYTE reg8_1;

    reg32_1 = (DWORD)cArray;
    reg32_2 = (DWORD)reg32_1 + 1;

    label1:
    reg8_1 = *(BYTE*)reg32_1;
    reg32_1++;

    if (reg8_1 != 0)
    {
        goto label1;
    }
    else
    {
        reg32_1 -= reg32_2;
        return reg32_1;
    }
}
lel

It's not recursive however
 
Ahh sorry, my bad.
Nice solution, will battle with this some more later on, but this is probably most efficient way of doing it with recursive func.
 
Might as well post a solution for c-strings:
Code:
size_t rstrlen(char *pstr)
{
  if (!*pstr) return 0;
  return rstrlen(pstr + 1) + 1;
}
 

Has Sysnative Forums helped you? Please consider donating to help us support the site!

Back
Top