Link to home
Start Free TrialLog in
Avatar of bimal_linux
bimal_linux

asked on

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
Avatar of Roonaan
Roonaan
Flag of Netherlands image

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

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
@system_down: have you tested it? (count should be a reserved function).

-r-
Avatar of bimal_linux
bimal_linux

ASKER

substr is a bulitin function. Cant use that
Strange question for an interview board though. You would expect they would spent their time on more useful questions.
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
     
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;
}

$len=0;while($str[$len])++$len;
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

ASKER CERTIFIED SOLUTION
Avatar of ed987
ed987

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
would this work?

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

<?php

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

?>
preg_match is a function... thus not allowed
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"


Sorry i noticed ed987  already posted this
Yours will not work when there's a 0 (number) in the string. Also noted by ed987...
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);

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

I hope you were joking?
who really cares?

quit taking yourself so seriously people
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
I found this:
"strlen is implemented as a standard extension function"

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

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

echo " " . $i;
?>

Bonmat86.
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
Yes, ed wrote:
$len=0;while($str[$len]!='')++$len;
Yes ed987!!

Bimal
Roonaans comment already mentioned this method in the first answer??