how to find string length without using counter

This is a discussion on how to find string length without using counter within the C Programming forums, part of the General Programming Boards category; hello all, how to find string length without using any counter? please provide me hints. Thank You, Nitin Mhetre....

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 11:29 AM
  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, 10:03 PM
  5. Basic C Programming Help Needed
    By Smurphygirlnz in forum C Programming
    Replies: 8
    Last Post: 09-26-2002, 07:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21