Implement String.prototype.toLowerCase()

2

2

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('');
}

Mohsen

Posted 2013-02-14T20:52:32.250

Reputation: 677

Winning condition? I saw String.prototype.toLowerCase.apply(new Date()); on StackOverflow with a quick search; what is the input going to be? – beary605 – 2013-02-15T01:00:28.080

@beary605 I've updated the question. I hope it's clear now. – Mohsen – 2013-02-15T04:22:40.757

1The example answer is buggy. It doesn't even handle the whole of Latin 1. – Peter Taylor – 2013-02-15T07:15:07.610

1It might want to be specified that the answer only has to work on ASCII characters. Otherwise, things may get out of hand quickly. – Mr. Llama – 2013-02-15T17:21:40.373

updated again. JavaScript supports unicode chars and involving that can be confusing. How would you lowercase Ú? – Mohsen – 2013-02-15T21:13:56.283

1ú, obviously. That's what String.prototype.toLowerCase() does. – Peter Taylor – 2013-02-15T22:14:08.013

Answers

5

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

Shmiddty

Posted 2013-02-14T20:52:32.250

Reputation: 1 209

Save 2 characters: for(s=i="" – copy – 2013-02-15T21:22:52.167

@copy didn't realize ''++ becomes 0 interesting. – Shmiddty – 2013-02-15T21:24:25.017

4

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

Mr. Llama

Posted 2013-02-14T20:52:32.250

Reputation: 2 387

Kindly show an example of input and output. – DavidC – 2013-02-15T22:30:22.063

shorter than golfscript. This is a first. Is this a first? – beary605 – 2013-02-16T00:49:36.167

2

Golfscript, 17

{..64>\91<*32*|}%

copy

Posted 2013-02-14T20:52:32.250

Reputation: 6 466

2

J, 25

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

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

randomra

Posted 2013-02-14T20:52:32.250

Reputation: 19 909

1

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
};

grc

Posted 2013-02-14T20:52:32.250

Reputation: 18 565

1

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

beary605

Posted 2013-02-14T20:52:32.250

Reputation: 3 904

0

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

Gaffi

Posted 2013-02-14T20:52:32.250

Reputation: 3 411

0

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"

manatwork

Posted 2013-02-14T20:52:32.250

Reputation: 17 865

0

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)
    });
}

gion_13

Posted 2013-02-14T20:52:32.250

Reputation: 101

0

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)

M L

Posted 2013-02-14T20:52:32.250

Reputation: 2 865