Take the 2-minute tour ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

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('');
}
share|improve this question
    
Winning condition? I saw String.prototype.toLowerCase.apply(new Date()); on StackOverflow with a quick search; what is the input going to be? –  beary605 Feb 15 '13 at 1:00
    
@beary605 I've updated the question. I hope it's clear now. –  Mohsen Feb 15 '13 at 4:22
1  
The example answer is buggy. It doesn't even handle the whole of Latin 1. –  Peter Taylor Feb 15 '13 at 7:15
1  
It might want to be specified that the answer only has to work on ASCII characters. Otherwise, things may get out of hand quickly. –  GigaWatt Feb 15 '13 at 17:21
1  
ú, obviously. That's what String.prototype.toLowerCase() does. –  Peter Taylor Feb 15 '13 at 22:14
show 1 more comment

9 Answers

up vote 5 down vote accepted

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

share|improve this answer
    
Save 2 characters: for(s=i="" –  copy Feb 15 '13 at 21:22
    
@copy didn't realize ''++ becomes 0 interesting. –  Shmiddty Feb 15 '13 at 21:24
add comment

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
share|improve this answer
    
Kindly show an example of input and output. –  David Carraher Feb 15 '13 at 22:30
    
shorter than golfscript. This is a first. Is this a first? –  beary605 Feb 16 '13 at 0:49
add comment

J, 25

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

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

share|improve this answer
add comment

Golfscript, 17

{..64>\91<*32*|}%
share|improve this answer
add comment

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
};
share|improve this answer
add comment

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).

share|improve this answer
add comment

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
share|improve this answer
add comment

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"
share|improve this answer
add comment

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)
    });
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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