Thread: is there a better option to strlen()

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    20

    is there a better option to strlen()

    It is my understanding that 'strlen()' is recursive and is not the most efficient way to derive the length of a string. Would it be better to just use the maximum size of the string instead? Is there a more efficient way to do this?


    Code:
    char buffer[15]
    
    ~ code
            for(int i=0; i<strlen(buffer);i++)
       { if(isdigit(buffer[i]==0)
            {cout<< "This is not a valid number";}
        }
        
    // Or should I use 
    
    for(int i=0;i<15;i++)
       { if(isdigit(buffer[i])==0)
            {cout<< "This is not a valid number";}
        }
    
    ~ code

  2. #2
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    If possible (depends what you want to do with the string) use a std::string, which has it's own length() function. It probably works in a similar way to strlen() actually, but I don't know.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > It is my understanding that 'strlen()' is recursive
    Then you are wrong - only the most lame-brained student exercise would make it recursive.

    Your use of it however is very inefficient.

    Try
    for(int i=0;buffer[i] != '\0';i++)
    If you want to step through a string.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    You could just use a loop like:
    Code:
    for (int i=0;buffer[i];i++)
    {
      //...
    }
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    20
    Thank you everyone.

    I appreciate the help.

    ~Rich

  6. #6
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    Code:
    int strlen(char *inp)
    {
        char *p = inp;
    
        while(*p)
            p++;
    
        return p - inp;
    }
    there is no real better alternative to strlen, but it essentially does the same as the above code.

  7. #7
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Quote Originally Posted by ahluka
    If possible (depends what you want to do with the string) use a std::string, which has it's own length() function. It probably works in a similar way to strlen() actually, but I don't know.
    It's not defined in the C++ standard, so it depends on implementation. Some implementations of std::string implement strings with a structure that keeps the size of the string, how much space is reserved for the string, a pointer to the buffer of characters, and some other information. For example, my copy of Visual Studio .NET 2003 uses 28 bytes for the std::string struct, and if you look at the memory, you can see the length of the string hard-coded. Then the .length() member function takes a constant amount of time, every time. Also, this makes push_back and += much more efficient.

    But on the port of g++ that comes with Dev-C++ 5 Beta, the std::string class is simply a wrapper for a char *, which points to a nul-terminated array, so I imagine it would use strlen.

  8. #8
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Quote Originally Posted by Rashakil Fol
    But on the port of g++ that comes with Dev-C++ 5 Beta, the std::string class is simply a wrapper for a char *, which points to a nul-terminated array, so I imagine it would use strlen.
    Excuse me, this information is incorrect. The std::string class's content consisted of exactly one pointer, and it did indeed point to an array of characters. But the size of and amount of space allocated for the string could be found if you backed up this pointer eight and twelve bytes.

  9. #9
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Quote Originally Posted by Rashakil Fol
    But the size of and amount of space allocated for the string could be found if you backed up this pointer eight and twelve bytes.
    Aye, that's almost my suggestion for the OP. Use Pascal strings, not null-terminanted strings.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. Replies: 3
    Last Post: 12-06-2008, 07:54 PM
  3. For some reason I end up with memory leak...
    By RaDeuX in forum C Programming
    Replies: 10
    Last Post: 11-26-2008, 12:43 AM
  4. Binary Search Tree
    By penance in forum C Programming
    Replies: 4
    Last Post: 08-05-2005, 05:35 PM
  5. Just say NO to strlen.
    By anonytmouse in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 02-11-2005, 01:34 PM