Thread: how to find string length without using counter

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    28

    how to find string length without using counter

    hello all,

    how to find string length without using any counter?
    please provide me hints.

    Thank You,
    Nitin Mhetre.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, if we are talking about standar C strings, there's no other option - strings don't have a stored length, so there's no option but to "look through the string, and count up the number of characters until the end".

    But you don't have to write the code for it, as C standard string library contains the function strlen, which gives you the number of characters in the string (note: not the counting the zero byte at the end, so if you use to to figure out how much emmoery you need for a particular string, you need one additional character to indicate the end, so you need to add one to the length).

    You need to use
    Code:
    #include <string.h>
    --
    Mats

  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
    Hint: use recursion
    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
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Salem View Post
    Hint: use recursion
    Not very good for really long strings (inefficient, may cause crashes for REALLY long strings), but yes, that would "avoid a counter".

    --
    Mats

  5. #5
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Another way to avoid a counter is to take the address of the first character and then keep incrementing it until you hit a '\0'. Then take the address of that character and subtract the first from the second, giving you the number of characters in the string.

    I in *no* way suggest that as an actual approach! ;-)

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange string behavior
    By jcafaro10 in forum C Programming
    Replies: 2
    Last Post: 04-07-2009, 07:38 PM
  2. find a string based on the location of another string
    By rlilley in forum C Programming
    Replies: 3
    Last Post: 02-19-2009, 12:29 PM
  3. Find String length and Arraysearch
    By s.rajaram in forum C Programming
    Replies: 5
    Last Post: 10-03-2007, 02:28 AM
  4. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  5. Basic C Programming Help Needed
    By Smurphygirlnz in forum C Programming
    Replies: 8
    Last Post: 09-26-2002, 07:12 PM