Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

how to count no of words in a string in php without using string functions

<?php
$str = "Shivam Sumit Sushu";
$c = 0;
$i = 0;
for ($i=0; $i < strlen($str); $i++) { 
    // echo "".$str[$i]."<br>";
    if ($str[$i]==" ") {
        $c += 1;
    }
}
if ($str[$i - 1] != " ") {
    $c += 1;
}
echo "Words in '$str' are $c";
?>
 
PREVIOUS NEXT
Tagged: #count #words #string #php #string #functions
ADD COMMENT
Topic
Name
9+3 =