Thread: Strlen

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    28

    Strlen

    If I write some thing like this
    for(int i = 0 ; i < strlen(s) ; i++)

    will it be O(n^2) ?

    Also please tell me where can I see codes of standard library of C (GCC) in Ubuntu.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The complexity depends what the loop body does. It is, however, not a good idea to evaluate strlen(s) every iteration if you care about efficiency, unless the contents of s changes within the loop body. Try evaluating strlen(s) before the loop, once only.

    You can download the source for various versions of gcc from somewhere on this site. Somewhere among those source distribution will be the source for libc (if I remember the name correctly), which is gnu's corresponding implementation of the C standard library.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    I'm not exactly sure, but isn't your for loop actually O(n*i)?

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    28
    Thanks

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    Wouldn't it just be directly proportional to the string length (linear)?

    O(n)?

  6. #6
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    strlen(s) has O(n) run time, where n is the length of the string. strlen(s) is called n times, so yes, the loop is O(n^2). As grumpy said, you should call strlen(s) once before the loop.

    EDIT: strlen could theoretically be implemented with O(1) run time, such as on a processor that can find the first 0 byte in constant time, but this is only theoretical. Assume strlen is O(n).
    Last edited by christop; 06-08-2012 at 10:02 AM.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    Of course, missed that.

    Basically an O(n) nested in an O(n).

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    The reason "The complexity depends what the loop body does." is that the loop might say append characters onto the string s, or put a null-terminator earlier in the string, or it might have another loop inside this one etc.

    Without anything special going on, it is as was originally guessed.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Registered User
    Join Date
    May 2012
    Posts
    505
    Quote Originally Posted by _arjun View Post
    If I write some thing like this
    for(int i = 0 ; i < strlen(s) ; i++)

    will it be O(n^2) ?
    Yes. strlen steps through the sgtring character by character to find the nul. Your callin it N times. So that's N squared operations.

  10. #10
    Registered User
    Join Date
    Sep 2011
    Posts
    28
    Thank you all guys

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strlen
    By krakatao in forum C Programming
    Replies: 2
    Last Post: 02-08-2012, 04:47 PM
  2. strlen()
    By exoeight in forum C Programming
    Replies: 9
    Last Post: 04-01-2005, 10:18 AM
  3. 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
  4. Strlen(...)
    By Korhedron in forum C++ Programming
    Replies: 6
    Last Post: 06-10-2003, 03:02 PM
  5. Why O why, strlen?
    By Sebastiani in forum C Programming
    Replies: 11
    Last Post: 08-24-2001, 01:41 PM