CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Mar 2000
    Location
    Seattle , WA
    Posts
    87

    Is it possible to implement a strlen function without declaring any variable.

    Hi,

    Is it possible to implement a strlen function without declaring any local or global variable ?

    Thanks,
    Last edited by arunkumar_gona; October 2nd, 2002 at 02:00 PM.

  2. #2
    Join Date
    Aug 2000
    Location
    New Jersey
    Posts
    968
    Here's one example:

    const char* TermPos(const char* Data)
    {
    while(Data[0]) ++Data;
    return Data;
    }

    int StrLen(const char* Data)
    {
    return TermPos(Data) - Data;
    }
    David Maisonave
    Author of Policy Based Synchronized Smart Pointer
    http://axter.com/smartptr


    Top ten member of C++ Expert Exchange.
    C++ Topic Area

  3. #3
    Join Date
    Aug 2000
    Location
    New Jersey
    Posts
    968
    Here's another example:

    int StrLen(const char* Data)
    {
    struct TermPos{
    const char* GetTermPos(const char* Data){while(Data[0]) ++Data;return Data;};
    };
    return TermPos().GetTermPos(Data) - Data;
    }
    David Maisonave
    Author of Policy Based Synchronized Smart Pointer
    http://axter.com/smartptr


    Top ten member of C++ Expert Exchange.
    C++ Topic Area

  4. #4
    Join Date
    Mar 2000
    Location
    Seattle , WA
    Posts
    87
    Great.

    Thanks,

  5. #5
    Join Date
    Aug 2000
    Location
    New Jersey
    Posts
    968
    Here's a real simple one using recursion.

    int StrLen(const char* Data)
    {
    if (!Data || !Data[0]) return 0;
    return 1 + StrLen((++Data));
    }
    David Maisonave
    Author of Policy Based Synchronized Smart Pointer
    http://axter.com/smartptr


    Top ten member of C++ Expert Exchange.
    C++ Topic Area

  6. #6
    Join Date
    Aug 2002
    Posts
    78
    Then there's the solution that requires neither recursion nor two functions:

    Code:
    int myStrLen(const char *s)
    {
         if (!s[0])
              return 0;
         if (!s[1])
              return 1;
    ...
         if (!s[MAX_POINTER_VALUE])
              return MAX_POINTER_VALUE;
    }
    although it's rather pathological. It's syntactically correct, but would end up being larger than the address space in which it operated, assuming you could even compile it, which you couldn't for much the same reason (if the compiler were on the same processor as the code.)


    Then there's this which doesn't actually work for two reasons:

    Code:
    int myStrLen(const char *d)
    {
    	return (int) d -
    nextd:
    	(!d[0] ? d++, goto nextd : d);
    }

    It's also kind of sad that the const in "const char *s" stops you from doing any funny business using the buffer at s as a variable holder.
    Last edited by Gorgor; October 2nd, 2002 at 01:22 PM.

  7. #7
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    Actually, just let me ask... why ??? Why do you want to do this in the first place ? Or is this a riddle ?

  8. #8
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Originally posted by Gorgor
    Then there's the solution that requires neither recursion nor two functions:
    And then there is the easiest solution...use 'strlen()'...

  9. #9
    Join Date
    May 2002
    Location
    Quebec City, Canada
    Posts
    374
    And then there is std::string...

    Why bother with char * anymore?
    Martin Breton
    3D vision software developer and system integrator.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured