string length without using builtin functions

Hi,

How to find the length of a string variable without using strlen or any other built  in functions?

Any suggestions will be greately appreciated. I got this question from an interview board.

Thanks in advance,

Bimal Mankara
LVL 6
bimal_linuxAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RoonaanCommented:
You can make a loop taking the first character until the string matches "".
You can split the string and count the array elements.

system_downCommented:
It's ok like this?


function count($string, $inc)
{
$item = substr($string, $inc, 1);
if($item != "")
{
$inc++;
$counter = contaci($string, $inc);
return $counter;
}
else
{
return $inc;
}


let me know
S_D
RoonaanCommented:
@system_down: have you tested it? (count should be a reserved function).

-r-
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

bimal_linuxAuthor Commented:
substr is a bulitin function. Cant use that
RoonaanCommented:
Strange question for an interview board though. You would expect they would spent their time on more useful questions.
bimal_linuxAuthor Commented:
its just a simple question. All other prog. languages I know can do this. Why not php?? There must be a way?? just curious to know thatz all..

Bimal Mankara
system_downCommented:
     
Comment from Roonaan
Date: 09/08/2006 01:54PM IST
      Comment       

You can make a loop taking the first character until the string matches "".
You can split the string and count the array elements.


Comment from system_down
Date: 09/08/2006 02:28PM IST
      Your Comment       

Sorry, i writed it bad...
It should be like:

function count_char($string, $inc)
{
$item = substr($string, $inc, 1);
if($item != "")
{
$inc++;
$counter = count_char($string, $inc);
return $counter;
}
else
{
return $inc;
}

ed987Commented:
$len=0;while($str[$len])++$len;
system_downCommented:
ok forget all what I writed...

It is impossible to get the string lenght of a string without builtin function... IMPOSSIBLE.. there is no '\0' @ the end of the string and you cannot know exactly when you have to stop to read...

only to correct my stupid post the right code is like that...

<?php

function count_char($string, $inc)
{
      $item = substr($string, $inc, 1);
      if($item != "")
      {
            $inc++;
            $counter = count_char($string, $inc);
            return $counter;
      }
      else
      {
            return $inc;
      }
}

$string = "leader";

echo $string . " -> number of char:  ";

echo count_char($string, 0);

?>

I excuse myself for the bad post...

Let me know.
S_D

ed987Commented:
sorry, that would fail on 0 eg in $str='test01';

fixed:

$len=0;while($str[$len]!='')++$len;
echo $len;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
savvior1Commented:
would this work?

$count=0;
foreach ($string as $k => $v) $count++;
TeRReFCommented:
It's a bit overkill, but it works :)

<?php

$string = 'Hello World';
preg_match('/$/', $string, $m, PREG_OFFSET_CAPTURE);
echo 'Length: '.$m[0][1];

?>
savvior1Commented:
preg_match is a function... thus not allowed
savvior1Commented:
I tested this and it works
<?
$string="THIS IS A STRING";

$i=0;
while($string[$i])
{
      $i++;
      
}


echo $i." ";

//compare results to strlen
echo strlen($string);

?>

output is "16 16"


savvior1Commented:
Sorry i noticed ed987  already posted this
TeRReFCommented:
Yours will not work when there's a 0 (number) in the string. Also noted by ed987...
savvior1Commented:
Ok this works, peace out.

 give me the points!

<?
$string="THIS IS 0 A STRING";

$i=0;
while(true)
{
      if ($string[$i] || $string[$i]=="0")
      {
      
            $i++;
      }
      else
      {
            break;
      }
}


echo $i." ";

echo strlen($string);

?>
ed987Commented:
my answer worked too and i posted over 4 hours before you did
TeRReFCommented:
@ savvior1
> give me the points!

I hope you were joking?
savvior1Commented:
who really cares?

quit taking yourself so seriously people
BogoJokerCommented:
You can get the source code for the strlen() function
Then get the source code for all the functions that strlen() uses
Then you will eventually get down to the most basic possible code.
Actually it looks to me like strlen() uses a C function that calculates the string length.
Sound like a plan? =)

Some people offered array alternatives.  This seems like a good way to go.  If you consider [] to not be a function.  Many languages, (ruby and I think c++) consider [] to be a function which you can manipulate to your own use.  If you don't consider [] to be a function then that would be the only way to go.  Count each character using a loop...

This question seems rather useless!  I would tell the interview board that this is a really ridiculous way to code.  One of the major principles of being a programmer is to NOT rewrite code.  Do not reinvent the wheel.   Because it is a very inefficient use of time, sometimes money, and so many other reasons.

Enjoy,
Joe P
BogoJokerCommented:
I found this:
"strlen is implemented as a standard extension function"

Joe P
Khanh DoanSenior DeveloperCommented:
<?php
$string = "My string";
echo strlen($string);
$i = 0;
$n = true;

while ($n)
{
      if ($string[$i] != NULL)
      {
            $i++;
      }
      else
      {
            $n = false;
      }
}

echo " " . $i;
?>

Bonmat86.
BogoJokerCommented:
bonmat that is the same as:

<?php
$length = 0;
while ($string[$length] != NULL)
  $length++;
echo $length;
?>

I think this strategy was already mentioned...
Joe P
BogoJokerCommented:
Yes, ed wrote:
$len=0;while($str[$len]!='')++$len;
bimal_linuxAuthor Commented:
Yes ed987!!

Bimal
TeRReFCommented:
Roonaans comment already mentioned this method in the first answer??
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.