2

I'm trying to create a function that returns TRUE if a string is all lowercase without using any built-in PHP functions. How can I do that?

This is what I was working with, but using ctype_lower.

$string = "string";

if (ctype_lower($string)) {
    echo $string . ' is all lowercase letters.';
} else {
    echo $string . ' is not all lowercase letters.';
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Notsrik
  • 51
  • 2
  • 6
  • 3
    Why don't you want to use built-in PHP functions? http://php.net/manual/en/function.ctype-lower.php – Charlotte Dunois Aug 16 '14 at 12:48
  • 2
    without using any built-in functions? out of curiosity, why? – vch Aug 16 '14 at 12:49
  • 2
    Do you need to handle ASCII only, or do you need to cover more (latin1, UNICODE, etc.)? If you need to cover full UNICODE, then this is quite difficult. – Austin Aug 16 '14 at 12:51
  • I'm new to PHP and i'm trying to practice as much as possible, would be possible to do that or it can be achieved only using built-in functions ? – Notsrik Aug 16 '14 at 12:55
  • @Notsrik that makes sense. Make sure you read this: http://blog.codinghorror.com/dont-reinvent-the-wheel-unless-you-plan-on-learning-more-about-wheels/ – ggirtsou Aug 16 '14 at 12:56
  • 1
    Please post the code you've come up so far. You shouldn't ask people for a solution, instead you need to show your research. – ggirtsou Aug 16 '14 at 12:59
  • 5
    @Notsrik You don't practice by asking others and not trying anything. – Ram Aug 16 '14 at 12:59
  • thank you, just my question with code that was using but with ctype_lower – Notsrik Aug 16 '14 at 13:28
  • Are you truly looking for a solution that doesn't use *any built-in PHP functions at all*, or did you simply mean that you don't want to use any functions such as `ctype_lower` which do all the work for you? – faintsignal Aug 16 '14 at 13:31

2 Answers2

11

Well, if you just want a simple-purpose check, you could just compare the original string with an strtolowerred string. Of course, it won't be foolproof. Like this:

function check_lowercase_string($string) {
    return ($string === strtolower($string));
}

$string = 'Hi! I am a string';
var_dump(check_lowercase_string($string)); // false
var_dump(check_lowercase_string('test')); // true

A painful attempt of no functions:

function check_lowercase_string($string) {
    $chars = '';
    // map all small characters
    for($alpha = 'a'; $alpha != 'aa'; $alpha++) { $small[] = $alpha; }
    $l = 0; // not strlen() :p
    while (@$string[$l] != '') {
        $l++;
    }
    for($i = 0; $i < $l; $i++) { // for each string input piece
        foreach($small as $letter) { // for each mapped letter
            if($string[$i] == $letter) {
                $chars .= $letter; // simple filter
            }
        }
    }

    // if they are still equal in the end then true, if they are not, false
    return ($chars === $string);
}

var_dump(check_lowercase_string('teSt')); // false
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kevin
  • 41,694
  • 12
  • 53
  • 70
  • Agree, no need of ternary operator at all. Just return the expression which will return boolean. – Rolice Aug 16 '14 at 13:01
  • thanks it results to boolean anyway – Kevin Aug 16 '14 at 13:01
  • Doesn't `strtolower` count as a builtin PHP function? – faintsignal Aug 16 '14 at 13:04
  • 1
    @faintsignal well which solution you want to propose? map out all the small characters and loop them and compare them each? a helpful advice would be appriciated – Kevin Aug 16 '14 at 13:06
  • Hey, I'm just pointing out what the OP asked for. – faintsignal Aug 16 '14 at 13:17
  • 1
    @faintsignal well i thought about it but it would be just weird having to loop all small caps and having them compare each piece, anyway maybe this question is best suited on codegolf – Kevin Aug 16 '14 at 13:18
  • 1
    @Ghost Agreed. I'm curious if (s)he overstated the requirement. Though you have to admit it becomes a more interesting, albeit unpractical, problem to solve if one literally cannot use any built-in functions. – faintsignal Aug 16 '14 at 13:21
  • `for($alpha = 'a'; $alpha != 'aa'; $alpha++) { $small[] = $alpha; }` What is this? – Rolice Aug 16 '14 at 13:38
  • @Rolice same concept as `range('a', 'z')`;, well cant use functions – Kevin Aug 16 '14 at 13:39
  • @Ghost, nice, I wasn't aware that PHP can do character incremental, like in other languages. However, I think it will be easier to operate with numeric representation, since ASCII representation of lowercase is between 97 and 122 and uppercase is in the range of 65 - 90. – Rolice Aug 16 '14 at 13:42
  • 1
    @Ghost Props for jumping through that flaming hoop, nicely done. Emphatic upvote. – faintsignal Aug 16 '14 at 13:43
  • @Rolice yeah it was weird when i saw it the first time either, yes strings can be incremented – Kevin Aug 16 '14 at 13:43
  • @Emissary man that was simpler haha! nice answer, you should post that too and mostly the correct answer – Kevin Aug 16 '14 at 13:50
0

You can either use ctype_lower, which is inbuilt, so you don't want that :c.

But you can use the answer from here. And put together a loop which checks for the first character. If uppercase then return true, and if not, then remove the first character of string and continue like that through the entire string.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
maht0rz
  • 96
  • 7