2
\$\begingroup\$

Implement String.prototype.toLowerCase()

Description

The toLowerCase method returns the value of the string converted to lowercase. toLowerCase does not affect the value of the string itself.

Conditions Let count chars inside function and ignore all bootstrap code:

String.prototype.toLowerCase = function(){
  // code that count here
}

Other languages are welcome but JavaScript implemantation is target of this question. Let's just focus on ASCII charachters .

Example input and output:

"sOmeText".toLowerCase(); => "sometext"

Example implementation:

String.prototype.toLowerCase = function(){
  return this.split('').map(function(c){
     var cc = c.charCodeAt(0);
     if (cc > 64 && cc < 91) {
        return String.fromCharCode(cc + 32);
     }
     return c;
  }).join('');
}
\$\endgroup\$
6
  • \$\begingroup\$ Winning condition? I saw String.prototype.toLowerCase.apply(new Date()); on StackOverflow with a quick search; what is the input going to be? \$\endgroup\$
    – beary605
    Feb 15, 2013 at 1:00
  • \$\begingroup\$ @beary605 I've updated the question. I hope it's clear now. \$\endgroup\$
    – Mohsen
    Feb 15, 2013 at 4:22
  • 1
    \$\begingroup\$ The example answer is buggy. It doesn't even handle the whole of Latin 1. \$\endgroup\$ Feb 15, 2013 at 7:15
  • 1
    \$\begingroup\$ It might want to be specified that the answer only has to work on ASCII characters. Otherwise, things may get out of hand quickly. \$\endgroup\$
    – Mr. Llama
    Feb 15, 2013 at 17:21
  • 1
    \$\begingroup\$ ú, obviously. That's what String.prototype.toLowerCase() does. \$\endgroup\$ Feb 15, 2013 at 22:14

10 Answers 10

5
\$\begingroup\$

Javascript 70 68

Building from @grc's answer:

for(s=i='';c=this[i++];s+=(parseInt(c,36)||c).toString(36));return s

Convert character to integer then back (works since Number.toString(36) will always be lowercase)

Test case: http://jsfiddle.net/Vh2TW/2/

input:

console.log('ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890~`-=_+{}[]:;\\\'\"<>,.?/!@#$%^&*()abcdefghijklmnopqrstuvwxyz'.toLowerCase())

output:

abcdefghijklmnopqrstuvwxyz1234567890~`-=_+{}[]:;\'"<>,.?/!@#$%^&*()abcdefghijklmnopqrstuvwxyz 

Update: -2 characters, thanks @copy

\$\endgroup\$
2
  • \$\begingroup\$ Save 2 characters: for(s=i="" \$\endgroup\$
    – copy
    Feb 15, 2013 at 21:22
  • \$\begingroup\$ @copy didn't realize ''++ becomes 0 interesting. \$\endgroup\$
    – Shmiddty
    Feb 15, 2013 at 21:24
4
\$\begingroup\$

Bash, 10

Doesn't interface with javascript very well, but it works for everyday purposes.

tr A-Z a-z

Also, it's shorter than golfscript. (Booyah!)


Example Input:

echo "Hello World, asDFghJkL0123" | tr A-Z a-z

Output:

hello world, asdfghjkl0123
\$\endgroup\$
2
  • \$\begingroup\$ Kindly show an example of input and output. \$\endgroup\$
    – DavidC
    Feb 15, 2013 at 22:30
  • \$\begingroup\$ shorter than golfscript. This is a first. Is this a first? \$\endgroup\$
    – beary605
    Feb 16, 2013 at 0:49
2
\$\begingroup\$

Golfscript, 17

{..64>\91<*32*|}%
\$\endgroup\$
2
\$\begingroup\$

J, 25

toLowerCase =: u:@(+32*91&>*64&<)@(3&u:)

Converting to numbers (3&u:) and back u: is quite uneasy.

\$\endgroup\$
1
\$\begingroup\$

Javascript, 86

String.prototype.toLowerCase = function () {
    s="";for(i=0;c=this.charCodeAt(i++);s+=String.fromCharCode(c>64&c<91?c+32:c));return s
};
\$\endgroup\$
1
\$\begingroup\$

Python (45)

l=lambda x:''.join(chr(ord(i)+('@'<i<'[')*32)for i in x)

Pseudocode:

l=func(x) {
  return toString( iterate(i in x) { chr(ord(i) + IsUppercase(i) * 32) } )
}

Explanation:
('@'<i<'[')*32): IsUppercase. '@' - 64, 'A' - 65, 'Z' - 90, '[' - 91: If 64<ord(i)<91, then return True and multiply it by 32 (to get 32).

\$\endgroup\$
0
\$\begingroup\$

VBA - 77 82

Function l(s)
'Counting only the stuff between Function and End
For x=1 To Len(s):m=Mid(s,x,1):l=l & IIf(m Like"[A-Z]",Chr(Asc(m)+32),m):Next
End Function
\$\endgroup\$
0
\$\begingroup\$

Ruby: 32 characters

s.gsub(/[A-Z]/){($&.ord+32).chr}

Sample run:
(In 35 characters it can be made a method of String class, to work just as the JavaScript sample.)

irb(main):001:0> class String
irb(main):002:1>   def toLowerCase  
irb(main):003:2>     self.gsub(/[A-Z]/){($&.ord+32).chr}
irb(main):004:2>   end
irb(main):005:1> end
=> nil
irb(main):006:0> "sOmeText".toLowerCase      
=> "sometext"
\$\endgroup\$
0
\$\begingroup\$

javascript 76 chars:

replace(/[A-Z]/g,function(b){return String.fromCharCode(b.charCodeAt()+32)})

Here's the full function:

String.prototype.toLowerCase = function () {
    return this.replace(/[A-Z]/g, function(b){
        return String.fromCharCode(b.charCodeAt()+32)
    });
}
\$\endgroup\$
0
\$\begingroup\$

Julia, 49 chars

L(s)=ascii([s[i]+32(64<s[i]<91)for i=1:endof(s)])

for all chars s[i] if 64 < s[i] < 91 is true, add 32 to the ASCII character point (32*true=32). If it’s false, then (32*false=0), and nothing is changed.

example:

julia> str
"ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#abcdefghijklmnopqrstuvwxyz~"

julia> L(str)
"abcdefghijklmnopqrstuvwxyz1234567890!@#abcdefghijklmnopqrstuvwxyz~"

The default method in Julia is lowercase(str)

julia> lowercase(str)
"abcdefghijklmnopqrstuvwxyz1234567890!@#abcdefghijklmnopqrstuvwxyz~"

the difference in timing and memory allocation:

julia> @time L(str)
elapsed time: 1.0263e-5 seconds (792 bytes allocated)

julia> @time lowercase(str)
elapsed time: 6.693e-6 seconds (224 bytes allocated)
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.