Converting a string to lower-case (without built-in to-lower functions!)

27

3

The goal of this code-golf is to create a code that lets the user input an ASCII string (contains only printable ASCII characters), and your program outputs the lower-case variant of this string.

Important: you are NOT allowed to use a built-in function that converts the string (or just one character) to lowercase (such as ToLower() in .NET, strtolower() in PHP , ...)! You're allowed to use all other built-in functions, however.

Another important note: The input string doesn't contain only uppercase characters. The input string is a mix of uppercase characters, lowercase characters, numbers and other ASCII printable characters.

Good luck!

ProgramFOX

Posted 2013-10-06T13:34:59.013

Reputation: 8 017

4unfortunately, I'll have to opt-out. I'm not a beginner. – John Dvorak – 2013-10-06T13:42:40.390

@Jan: Well, with beginner I actually meant that the skill level of this would be 'beginner', not that only beginners would be allowed to enter. I removed the word 'beginner' and surely, you're allowed to enter. – ProgramFOX – 2013-10-06T13:45:04.603

1Are regular expressions allowed? Only GolfScript could beat s/./\L\0/g. – manatwork – 2013-10-06T13:49:59.880

3@manatwork: surely \L is built in? – marinus – 2013-10-06T13:50:30.260

@manatwork: Yes, a regex is allowed. – ProgramFOX – 2013-10-06T13:50:35.480

@marinus is builtin, but not a function. (This would be a function in the regular expression: s/./lc$&/ge, so this one clearly not allowed.) – manatwork – 2013-10-06T13:52:19.597

@ProgramFOX, can we assume the input will contain only uppercase letters and no other characters? – manatwork – 2013-10-06T14:54:06.043

@manatwork: No, the input will contain uppercase letters, lowercase letters, numbers, and some other ASCII characters. – ProgramFOX – 2013-10-06T15:13:14.927

May the input string contain control characters such as tab, line break, carriage return, or is only the range of ASCII printable characters valid input? What about null bytes, do they have to be replicated in the output?

– FireFly – 2013-10-06T15:44:32.560

@FireFly: Only printable characters. I didn't mention that in the question, so it's updated now. – ProgramFOX – 2013-10-06T15:46:22.237

Answers

21

Shell - 10

Translation of @Gowtham's Perl solution using /bin/tr.

tr A-Z a-z

Sample run:

% tr A-Z a-z <<<'Hello WORLD! @'
hello world! @

FireFly

Posted 2013-10-06T13:34:59.013

Reputation: 7 107

What makes this the accepted answer, out of curiosity? Gowtham had a 10-character solution first… – Ry- – 2013-10-10T01:25:57.397

1

Based on the discussion on meta it seems the reasoning is that Gowtham's solution is 11 chars (because the -p flag counts as one). I agree though, his seems like it deserves more to be accepted..

– FireFly – 2013-10-10T05:57:41.207

Ah, thanks – that makes sense. I’ll keep it in mind! – Ry- – 2013-10-10T14:32:44.610

53

Python 2.7 - 30 (with terrible and unapologetic rule abuse)

raw_input().upper().swapcase()

As an anonymous edit pointed out, you can do it in 27 26 in Python 3:

input().upper().swapcase()

I'm flagrantly abusing the rules here, but...

Important: you are NOT allowed to use a built-in function that converts the string (or just one character) to lowercase (such as ToLower() in .NET, strtolower() in PHP , ...)! You're allowed to use all other built-in functions, however.

This takes the strings and coverts it to upper case. Then in a very unrelated method call, it reverses the case of the string - so that any lower case letters become upper case letters... and swaps any upper case letters to lower case letters.

user8777

Posted 2013-10-06T13:34:59.013

Reputation:

1The Python 3 solution is 26 characters. – Timtech – 2013-12-08T12:50:37.170

@Timtech I can't count. – None – 2013-12-08T23:48:33.407

1It's not just unrelated. It's very unrelated. – Carter Pape – 2014-08-20T02:34:07.397

1This will have strange results when encountering text that contains the characters ß. – FUZxxl – 2014-08-23T14:26:44.427

33

Perl - 11 10 characters.

y/A-Z/a-z/

y/// is same as tr///!

In action:

% perl -pe 'y/A-Z/a-z/' <<< 'Hello @ WORLD !'
hello @ world !

Gowtham

Posted 2013-10-06T13:34:59.013

Reputation: 571

3+1, for the only real-life language that beat out all the less(?) real ones. – Behrooz – 2013-10-06T19:57:59.953

Actually that is 11 characters. The -p option is counted as 1. – manatwork – 2013-10-08T07:56:08.293

@manatwork Or it should be counted as 2 : - and p :) – Gowtham – 2013-10-08T13:15:30.077

1 if you assume -e (perl -e -> perl -pe), 3 if you assume a script (perl -> perl -p). – nyuszika7h – 2014-08-20T09:46:12.430

10

Befunge-98 - 26 22 21 19

~:''-d2*/1-!' *+,#@

Relies on the fact that (c-39)/26 is 1 only for character codes of uppercase ASCII characters (assuming integer division). For each character c, print out c + (((c-39)/26)==1)*' '.

Sample session:

% cfunge lower.b98
hello WORLD!
hello world!
This is a TEST!!11 az AZ @[`{
this is a test!!11 az az @[`{

FireFly

Posted 2013-10-06T13:34:59.013

Reputation: 7 107

9

Python 3, 58

print("".join(chr(ord(x)+('@'<x<'[')*32)for x in input()))

Ry-

Posted 2013-10-06T13:34:59.013

Reputation: 5 283

Can you explain how this works? I'm really interested in getting better at Python. I don't get how the map(ord,input()) bit works. – asteri – 2013-10-06T17:34:44.273

1@JeffGohlke: map applies a function (in this case, ord) to an interable and returns an iterable. It’s like a shorter form of (ord(x) for x in input()). – Ry- – 2013-10-06T18:21:46.960

Got it. Thanks for the explanation! – asteri – 2013-10-06T18:25:03.253

1Your answer follows the spirit of the question, but mine follows the letter of the question... – None – 2013-10-07T05:55:33.527

Very nice. Beat my unposted 62 length solution for c in input():print([c,(chr(ord(c)+32))]['@'<c<'['],end=''). I tried some with the map(ord,input()) trick, but missed the multiplying the truth value by 32 and adding it to the character code trick. Very nice. – Steven Rumbalski – 2013-10-08T18:56:50.493

8

Ruby, 18 characters

Nothing really interesting.

gets.tr'A-Z','a-z'

(run in IRB)

Just for fun: a confusing version:

$,=$* *' ';$;=$,.tr'A-Z','a-z';$><<$;

Run like this:

c:\a\ruby>lowercase.rb Llamas are AMAZING!

Output

llamas are amazing!

Doorknob

Posted 2013-10-06T13:34:59.013

Reputation: 68 138

7

J - 30

'@Z'(]+32*1=I.)&.(a.&i.)1!:1]1

J is read right-to-left, so to break this down:

  1. Prompt user for input: 1!:1]1
  2. Perform algorithm in code-point-space: &.(a.&i.)
  3. Identify character range for each letter; the characters between codepoints "@" and "Z" are considered uppercase: 1=I..
  4. For each uppercase codepoint, add 32: ]+32* ...
  5. Note that step (2) creates an implicit step (5): we started out by projecting from character to integer domain, so now that we're finished, we map those integers back onto characters.

Obviously this particular implementation only considers ASCII; but the approach could be extended to at least the basic multilingual plane in Unicode.

Dan Bron

Posted 2013-10-06T13:34:59.013

Reputation: 421

1Nice! Unfortunately, it seems your solution is going the WRONG WAY. ;-) Should be an easy fix though. (Edit: '@Z'(]+32*1=I.)&.(a.&i.)1!:1]1 should do it) – FireFly – 2013-10-06T16:33:31.453

Nice catch, thanks. I'm also impressed you were able to fix the code yourself: J isn't the most immediately-accessible language out there :) – Dan Bron – 2013-10-06T16:45:17.713

Ah, I've played around some with J myself.. I managed to come up with u:(a.i.x)+32*1='@Z'I.x=.1!:1]1, which matches your length but is much less interesting (as it doesn't make use of 'under'). Speaking of which, I didn't know about dyadic I., so thanks for using that. :-) – FireFly – 2013-10-06T16:49:17.807

Cool. But your Befunge solution still has J beat by 4 characters. Obviously I can't let that stand :) I'm trying to see if trim the J solution down by following your lead in relying solely on '@', rather than both '@' and 'Z'. – Dan Bron – 2013-10-06T16:57:41.483

(32(23)b.])&.(3&u:), should be 5 bytes shorter. – FrownyFrog – 2017-10-20T00:12:09.933

23 b.&32&.(3&u:) – FrownyFrog – 2017-10-20T00:18:07.740

7

C 64 63 59 55 chars

main(c){while(c=getchar(),~c)putchar(c-65u<27?c+32:c);}

Rozuur

Posted 2013-10-06T13:34:59.013

Reputation: 501

I count only 63 characters there. – manatwork – 2013-10-07T09:26:09.493

You can lose 9 characters: drop int and ,c>=0. They're not necessary here. – JoeFish – 2013-10-07T18:00:57.673

we need c>=0 as getchar(EOF) will be < 0. Thanks for other suggestion. – Rozuur – 2013-10-07T21:20:10.387

2>

  • ~(c=getchar()) 2. c-64u<27
  • < – ugoren – 2013-10-08T08:28:03.693

    Thats great, have incorporated your suggestions. Thanks – Rozuur – 2013-10-08T09:13:10.717

    You can save one more by c=~getchar(). It will require some work on your putchar though. – ugoren – 2013-10-08T11:36:27.753

    1

    Insignificantly small bug: seems there should be 65 instead of 64. http://pastebin.com/Zc9zMx2W

    – manatwork – 2013-10-09T16:03:58.060

    5

    Golfscript - 17

    Program:

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

    Explanation:

    1. {}% maps the code inside to every character in string.
    2. .. copies the top of the stack (the character) twice.
    3. 64> 1 if character code is greater than 64, else 0.
    4. \ swaps the two items on the stack (gets the second copy of the letter, and stores the result of 64> in position two).
    5. 91< checks to see if character code is less than 91. Similar to step 3.
    6. * multiplies the results from steps 3 and 5 together. Only equal to 1, if both steps were true.
    7. 32* multiplies the result of step 6 with 32. Will be 32 if step 6 was 1, else 0.
    8. + add the result (either 32 or 0) onto the character code.

    Example output:

    echo HelLO @ WorLD | ruby golfscript.rb upper_to_lower.gs
    hello @ world
    

    Rees

    Posted 2013-10-06T13:34:59.013

    Reputation: 111

    4

    Perl: 24 characters

    s/[A-Z]/chr 32+ord$&/ge
    

    Sample run:

    bash-4.1$ perl -pe 's/[A-Z]/chr 32+ord$&/ge' <<< 'Hello @ WORLD !'
    hello @ world !
    

    manatwork

    Posted 2013-10-06T13:34:59.013

    Reputation: 17 865

    Hem, why chr ord ? I'm pretty sure you won't learn anything in reading my answer ;-) – F. Hauri – 2013-12-04T17:46:45.560

    Amazing trick, @F.Hauri! – manatwork – 2013-12-04T18:00:03.193

    @nyuszika7h, the +1 is the -p command line parameter, not a newline. – manatwork – 2014-08-20T12:16:09.137

    Oh right, sorry. – nyuszika7h – 2014-08-20T12:16:39.067

    3

    javascript 80

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

    (76 if you remove "X")

    with prompt and alert - 92

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

    fiddle

    thanks to @FireFly @some @C5H8NNaO4 and @minitech

    Math chiller

    Posted 2013-10-06T13:34:59.013

    Reputation: 337

    Er, you'd need to wrap the second argument to replace with function($){return ...}, no? By the way, the first param to the replacement function is the matched string, so you could drop the parens in the regex. – FireFly – 2013-10-07T08:00:04.520

    How would i go about running it,like this? – C5H8NNaO4 – 2013-10-07T08:37:51.873

    @C5H8NNaO4 str(code here) – Math chiller – 2013-10-07T10:11:47.453

    I guess for a valid input you should at least assume, it, to be predefined. costs only a char – C5H8NNaO4 – 2013-10-07T10:58:28.013

    @C5H8NNaO4 three (1)"(2)char(3)", its already pretty long, and the other answers dont have it. u can test this code if u want to see it run though "TeSt".replace(/[A-Z]/,function($){return String.fromCharCode($.charCodeAt()+32)}) – Math chiller – 2013-10-07T11:58:49.370

    6I think all (or at least most) answers in here read from stdin and print to stdout. From what I gather the convention is to use prompt and alert for I/O in JS. – FireFly – 2013-10-07T14:17:25.590

    If you had tested your code, you would have fond that it only converts the first upper case character, not all of them. It's 92 characters, when following the rules: alert(prompt().replace(/[A-Z]/g,function(a){return String.fromCharCode(a.charCodeAt()+32)})) – some – 2013-12-05T13:21:04.760

    1You need a /g flag for this to work properly. – Ry- – 2013-12-07T14:36:23.130

    @some there is no rule which requires alert and prompt, but i will edit in that as well – Math chiller – 2013-12-09T21:00:11.120

    @tryingToGetProgrammingStraight Somehow you must get the user supplied string into the program, and show the result. When code golfing in javascript the common way is to use prompt and alert. The rules doesn't say anything about it since the rules are language agnostic. If you look at the other answers you will see that they use the equivalent statements to do input/output in their respective languages. So why do you think that you don't need it when writing in javascript? – some – 2013-12-10T00:24:51.300

    @some i figured that maybe it could be part of the program (as in my example), but since u commented i added one with prompt and alert as well, i dont see anything wrong with it now. – Math chiller – 2013-12-10T02:52:44.053

    @tryingToGetProgrammingStraight Well, in that case I can write a program that is 0 characters long, pretending that everything else somehow is part of the program. Does that feel fair? Now your code works since it almost a verbatim copy of what I wrote, but you are using $ as a variable name. While it is legal, it is a bit confusing since $ mostly is used by libraries like jQuery. I also noticed that you didn't credit me or anyone else for finding bugs in your code. – some – 2013-12-10T12:43:22.783

    It's 80 bytes, not 79. – nyuszika7h – 2014-08-20T10:21:45.840

    3

    JavaScript - 109 104 (ES6: 95)

    Thanks to some for the corrected version.

    a=prompt();for(b=[i=0];c=a.charCodeAt(i);)b[i++]=String.fromCharCode(c|(c>64&c<91)*32);alert(b.join(""))
    

    The following works if the browser supports ES6 function expressions:

    alert(prompt().split("").map(c=>String.fromCharCode(c.charCodeAt()|(c>"@"&c<"[")*32)).join(""))
    

    FireFly

    Posted 2013-10-06T13:34:59.013

    Reputation: 7 107

    The first code doesn't work (tested in FF and Chrome) because when trying to get a character after the length of the string, you get undefined and then c.charCodeAt() fails because undefined don't have charCodeAt. A working example 105 characters: a=prompt();for(b=[i=0];c=a.charCodeAt(i);)b[i++]=String.fromCharCode(c|(c>64&&c‌​<91)*32);alert(b.join('')) – some – 2013-12-05T13:29:13.870

    @some oops, I wonder how I came up with that snippet.. I'm pretty sure I tested that code, maybe I copied a non-working version in or something. Anyway, thanks for the correction. – FireFly – 2013-12-05T23:55:51.943

    Using a bitwise and instead of a logical one... nice! – some – 2013-12-06T00:10:33.410

    An even more ES6 solution (79): L=s=>[String.fromCharCode(c.charCodeAt()|(c>"@"&c<"[")*32)for(c of s)].join(''). Usage: L('SoMeTeXt') – Florent – 2014-02-28T15:07:30.277

    Nice! I'm not sure about making it a mere function though, since all other solutions are "proper" programs. Still, very nice use of for..of regardless. – FireFly – 2014-02-28T17:13:44.893

    3

    DELPHI

    const
      UpChars:set of AnsiChar = ['A'..'Z'];
    var
      I: Integer;
    begin
      SetLength(Result, Length(pString));
      for I := 1 to length(pstring) do
        Result[i] := AnsiChar((Integer(pString[i] in UpChars))*(Ord(pString[i])+32));
      WriteLn(Result);
    end;
    

    Fabricio Araujo

    Posted 2013-10-06T13:34:59.013

    Reputation: 139

    3This is not golf. Don't you feel this piece is very different compared to others ? – Ray – 2013-10-08T16:57:37.757

    1@ray Golfing is about getting your code as short as possible. Delphi isnt a great language for golfing. I use delphi myself and even though there isnt a big chance I could win a golf with delphi, its still fun to challenge yourself. – Teun Pronk – 2014-02-27T07:59:38.830

    3

    Python (33)

    If in doubt, use the shell.

    import os;os.system('tr A-Z a-z')
    

    Regrettably, this is still longer than Lego's solution.

    Lambda Fairy

    Posted 2013-10-06T13:34:59.013

    Reputation: 311

    +1 That is indeed not a Python built-in you are using. Only works on linux, but still very rule-bendy!!! – None – 2013-10-09T03:31:52.410

    @LegoStormtroopr Works everywhere there is a tr command (which does the right thing) on the path of the invoked shell, I suppose. – Paŭlo Ebermann – 2013-10-13T09:41:32.387

    3

    Perl 18

    s/[A-Z]/$&|" "/eg
    

    Something like:

    perl -pe 's/[A-Z]/$&|" "/eg'  <<<'are NOT allowed to: ToLower() in .NET, strtolower() in PHP'
    are not allowed to: tolower() in .net, strtolower() in php
    

    and

    perl -pe 's/[A-Z]/$&|" "/eg' <<< "The input string Doesn't cOntaIn...( C0D3-@01F. ;-)"
    the input string doesn't contain...( c0d3-@01f. ;-)
    

    For @FireFly :

    perl -pe 's/[A-Z]/$&|" "/eg' <<< "Doesn't this translate @ to \` and [\]^_ to {|}~DEL? "
    doesn't ... @ to ` and [\]^_ to {|}~del? 
    

    no.

    More generic: 18 chars anyway:

    s/[A-Z]/$&|" "/eg
    

    s/[A-Z]/$&^" "/eg
    

    This wont change anything in state:

    perl -pe 's/[A-Z]/$&^" "/eg' <<< "Doesn't ... @ to \` and [\]^_ to {|}~DEL? "
    doesn't ... @ to ` and [\]^_ to {|}~del? 
    

    All work fine, but the advantage of changing | (or) by ^ (xor) is that the same syntax could be used for toLower, toUpper or swapCase:

    toUpper:

    perl -pe 's/[a-z]/$&^" "/eg' <<< "Doesn't ... @ to \` and [\]^_ to {|}~DEL? "
    DOESN'T ... @ TO ` AND [\]^_ TO {|}~DEL? 
    

    and swapCase (18+1 = 19 chars):

    perl -pe 's/[a-z]/$&^" "/egi' <<< "Doesn't ... @ to \` and [\]^_ to {|}~DEL? "
    dOESN'T ... @ TO ` AND [\]^_ TO {|}~del? 
    

    F. Hauri

    Posted 2013-10-06T13:34:59.013

    Reputation: 2 654

    I forgot +1 for -p sorry @manatwork – F. Hauri – 2013-12-04T18:04:05.673

    Doesn't this translate @ to backtick and [\]^_ to {|}~DEL? And therein lies the tricky part.. – FireFly – 2013-12-04T18:15:26.670

    1@FireFly No, $& have to match [A-Z]. – F. Hauri – 2013-12-04T18:19:03.507

    Oh, my bad. Very cool, then! – FireFly – 2013-12-04T20:56:44.917

    2

    R

    71 characters:

    chartr(paste(LETTERS,collapse=""),paste(letters,collapse=""),scan(,""))
    

    83 characters:

    a=as.integer(charToRaw(scan(,"")))
    b=a%in%(65:90)
    a[b]=a[b]+32
    rawToChar(as.raw(a))
    

    plannapus

    Posted 2013-10-06T13:34:59.013

    Reputation: 8 610

    That's 86 characters - newlines count as 2 characters. (http://www.string-functions.com/length.aspx)

    – Timtech – 2013-12-08T00:42:06.310

    @Timtech: In R you can replace newlines in code by ; so no they count just for one character. It could be written: a=as.integer(charToRaw(scan(,"")));b=a%in%(65:90);a[b]=a[b]+32;rawToChar(as.raw(a)) – plannapus – 2013-12-09T08:26:12.617

    Yes, now I realized. I read up on meta... seems that only on Windows that newlines are 2 characters (I was using a program to measure the length of my code). – Timtech – 2013-12-09T11:49:30.663

    2

    Q, 18

    .....

    {x^ssr/[x]..Q`a`A}
    

    tmartin

    Posted 2013-10-06T13:34:59.013

    Reputation: 3 917

    2

    PHP (42)

    Run from the command line:

    -R'echo@str_ireplace($a=range(a,z),$a,$argn);'
    

    -R and the single quotes are not counted.

    PleaseStand

    Posted 2013-10-06T13:34:59.013

    Reputation: 5 369

    If you follow Gowtham's Peal solution, you would only count 42 characters. – eisberg – 2013-10-08T12:04:17.860

    1@eisberg: Updated the score, leaving a 43-character version in the history in case of any dispute. – PleaseStand – 2013-10-08T13:21:50.143

    str_ireplace does case insensitive search, which is stretching the rules, if not breaking them. – ugoren – 2013-10-08T13:55:42.850

    @ugoren I do not think so. As it is clearly stated that only build in function changing the case are not allowed and this is ignoring the case not changing it. – eisberg – 2013-10-09T13:24:01.130

    2

    Q (16)

    .......

    {x^(.Q.A!.Q.a)x}
    

    skeevey

    Posted 2013-10-06T13:34:59.013

    Reputation: 4 139

    2

    PowerShell: 69 65 64

    I've tried a half-dozen ways to get Replace to work the way I want it to without using the long [regex]::Replace syntax, but I haven't had any luck. If anyone else has an idea of what might work, please do suggest it.

    Golfed code:

    [regex]::Replace((read-host),"[A-Z]",{[char](32+[char]"$args")})
    

    Changes from original:

    • Rearranged last argument so that [int] is no longer needed, per suggestion in comments.

    Explanation:

    (read-host) gets the user input.

    [regex]::Replace(...) tells PowerShell to use RegEx matching to perform replacement operations on a string.

    "[A-Z]" matches all uppercase letters.

    {...} tells PowerShell to use a script to determine the replacement value.

    [char]"$args" takes the current match and types it as an ASCII character.

    32+ converts the character to an integer, representing the ASCII code, and increases the value by 32 - which would match ASCII code of the corresponding lowercase letter.

    [char](...) takes the resulting value and converts it back to an ASCII character.

    Demo of original:

    enter image description here

    (Current version tested - screenshot not yet posted.)

    Iszi

    Posted 2013-10-06T13:34:59.013

    Reputation: 2 369

    1Haven't checked on how to get around that [regex]::Replace, but you can save 4 chars by changing [int] to + – goric – 2013-12-04T21:19:29.473

    1Actually, the whole last argument can be rearranged to {[char](32+[char]"$args")}, which removes the need to explicitly cast to int and shaves off one more character – goric – 2013-12-04T21:42:12.497

    @goric Geez, why didn't I think of that already? Still learning, I guess. – Iszi – 2013-12-04T21:53:01.563

    2

    PowerShell, 53 49 43 bytes

    -4 bytes thanks @AdmBorkBork

    -6 bytes thanks @Veskah

    -join($args|%{[char](32*($_-in65..90)+$_)})
    

    Try it online!

    mazzy

    Posted 2013-10-06T13:34:59.013

    Reputation: 4 832

    cool! ¯\_(ツ)_/¯ – mazzy – 2018-09-20T16:43:55.537

    1Splat it up for 43 – Veskah – 2020-01-24T15:36:20.540

    2

    05AB1E, 3 bytes

    u.š
    

    Port of @user8777 Python 3 answer.

    Try it online.

    Explanation:

    u    # Convert the (implicit) input to uppercase
     .š  # Switch the case (upper to lower and vice-versa)
         # (and output the result implicitly)
    

    But without any case-altering builtins:

    05AB1E, 12 11 bytes

    ÇIS.u32*+çJ
    

    -1 byte thanks to @Emigna.

    Try it online.

    Explanation:

    Ç            # Get the unicode values of each character of the (implicit) input-String
     IS          # Get the input-string, split to characters again
       .u        # Check for each if it's uppercase or not (1 if truthy; 0 if falsey)
         32*     # Multiply that result by 32 (32 if truhy; 0 if falsey)
            +    # Add it to all the unicode values at the same indices in the list
             ç   # Convert the now modified unicode values back to characters
              J  # And join all characters together to a string again
                 # (which is output implicitly as result)
    

    Kevin Cruijssen

    Posted 2013-10-06T13:34:59.013

    Reputation: 67 575

    1ÇIS.u32*+çJ saves a byte on your 12-byte version. – Emigna – 2019-04-09T13:17:37.143

    @Emigna Ah, smart. I had tried the .u32*+ approach like this: εÇy.u32*+ç]J, but unfortunately ç wraps the characters in a list, so an additional J or was required after theç`.. – Kevin Cruijssen – 2019-04-09T13:23:31.817

    2

    k2, 15 bytes

    I am super late to this one, but I found this cool anyway.

    {_ci 32+_ic x}'
    

    Also:

    Pyth, 10 bytes

    Doesn't really count because Pyth was created after this was posted. Still cool.

    jkmC+32Cdw
    

    kirbyfan64sos

    Posted 2013-10-06T13:34:59.013

    Reputation: 8 730

    1

    Python 3 - 70

    Updated for OP's changes.

    I'm a Python newbie, so any critique is welcome.

    print("".join(chr(ord(c)+32) if 64<ord(c)<91 else c for c in input()))
    

    asteri

    Posted 2013-10-06T13:34:59.013

    Reputation: 824

    I'm sorry, I had to say that you're not allowed to use a to-lower function on one character. Question updated. – ProgramFOX – 2013-10-06T15:19:26.193

    1

    Please see my recent comment: your code does only work if the input string contains only uppercase characters, but please note that it also contain other ASCII characters such as lowercase characters and numbers.

    – ProgramFOX – 2013-10-06T15:22:20.993

    Okay, will update when I get home – asteri – 2013-10-06T16:58:00.253

    @ProgramFOX Updated. – asteri – 2013-10-06T17:34:13.633

    Jeff, check out @minitechs answer. You both have very similar approaches so you should be able to see how, and why his answer is shorter.

    – None – 2013-10-09T03:32:54.177

    1

    Javascript, 105

    prompt().split("").map(function(a){c=a.charCodeAt(0);return String.fromCharCode(c|(c-64?32:0))}).join("")
    

    Actually ther was no output form specified, so run it in console Yea, JavaScript really is verbose with charcode <-> string

    C5H8NNaO4

    Posted 2013-10-06T13:34:59.013

    Reputation: 1 340

    1c.charCodeAt() -- it defaults to 0 if an index is omitted. Also, breaks on '@' I believe (it gets "lowercased" into backtick) – FireFly – 2013-10-07T07:42:40.077

    @FireFly Nice, Thanks!, ok i'll gonna fix it =) – C5H8NNaO4 – 2013-10-07T07:44:20.553

    1

    Ruby: 66

    def l(s)s.bytes.map{|b|(65..90).include?(b)?b+32:b}.pack('c*');end
    

    KaptajnKold

    Posted 2013-10-06T13:34:59.013

    Reputation: 111

    1

    C# - 108

    class P{static void Main(string[]a){foreach(var c in a[0])System.Console.Write(
    (char)(c>64&&c<91?c+32:c));}}
    

    About 70 for just the method body.

    Add 5 chars to include a LF/CR in the output:

    class P{static void Main(string[]a){foreach(var c in a[0]+"\n")System.Console.Write(
    (char)(c>64&&c<91?c+32:c));}}
    

    A LINQ version would be shorter:

    class P{static void Main(string[]a){a[0].Any(c=>System.Console.Write(
    (char)(c>64&&c<91?32+c:c))is P);}}
    

    (103) .. except that it requires using System.Linq; (total: 121).

    Igby Largeman

    Posted 2013-10-06T13:34:59.013

    Reputation: 353

    1

    x86-16 machine code, 14 bytes

    Assembled:

    ac3c 417c 063c 5a7f 020c 20aa e2f2
    

    Unassembled listing:

           _LOOP:
     AC         LODSB           ; load byte from [SI] into AL, advance SI 
     3C 41      CMP  AL, 'A'    ; is char less than 'A'? 
     7C 06      JL   _STORE     ; if so, do not convert 
     3C 5A      CMP  AL, 'Z'    ; is char greater than 'Z'? 
     7F 02      JG   _STORE     ; if so, do not convert 
     0C 20      OR   AL, 020H   ; lowercase the char 
           _STORE:
     AA         STOSB           ; store char to [DI], advance DI 
     E2 F2      LOOP _LOOP      ; continue loop through string 
    

    Input string in [SI], length in CX. Output string in [DI].

    Output from PC DOS test program:

    enter image description here

    Download and test TOLOW.COM example program.

    640KB

    Posted 2013-10-06T13:34:59.013

    Reputation: 7 149

    where is the 14 byte count coming from? the snippet is longer than that, even without comments... is 14 bytes the compiled program? – Jonah – 2019-04-09T16:08:00.457

    1

    @Jonah The byte opcode is in the lefthand column, AC 3C 41, etc. I'll add the assembled hex byte code to the top for clarity. https://codegolf.meta.stackexchange.com/a/12340/84624

    – 640KB – 2019-04-09T16:18:12.457

    1

    naz, 92 bytes

    2a2x1v4m8m2x2v2d3m5s2x3v1x1f1r3x1v4e3x2v2g1o1f0x1x2f3x3v3l1o1f0x1x3f8a8a8a8a1o1f0x1x4f0a0x1f
    

    Works for any input string terminated with the control character STX (U+0002).

    Explanation (with 0x commands removed)

    2a2x1v                 # Set variable 1 equal to 2
    4m8m2x2v               # Set variable 2 equal to 64 ("@")
    2d3m5s2x3v             # Set variable 3 equal to 91 ("[")
    1x1f1r3x1v4e3x2v2g1o1f # Function 1
                           # Read a byte of input
                           # Jump to function 4 if it equals variable 1
                           # Jump to function 2 if it's greater than variable 2
                           # Otherwise, output it and jump back to the start of function 1
    1x2f3x3v3l1o1f         # Function 2
                           # Jump to function 3 if the register is less than variable 3
                           # Otherwise, output and jump to function 1
    1x3f8a8a8a8a1o1f       # Function 3
                           # Add 32 to the value in the register, output, and jump to function 1
    1x4f0a                 # Function 4
                           # Add 0 to the register
    1f                     # Call function 1
    

    sporeball

    Posted 2013-10-06T13:34:59.013

    Reputation: 461

    1

    Haskell - 58

    p x|(elem x['A'..'Z'])=[x..]!!32|1<2=x
    main=interact$map p
    

    nyuszika7h

    Posted 2013-10-06T13:34:59.013

    Reputation: 1 624

    1

    Perl, 9 + 1 (for -p flag) = 10

    $_="\L$_"
    

    \L was specifically asked about and allowed, because even though it's a built-in, it's not a function.

    CJ Dennis

    Posted 2013-10-06T13:34:59.013

    Reputation: 4 104

    0

    Just for fun, because Java's my language.

    Java - 162 175

    Fixed for OP's updates.

    class a{public static void main(String[]a){String b="";for(char c:new java.util.Scanner(
    System.in).nextLine().toCharArray())b+=c>64&&c<91?(char)(c+32):c;System.out.print(b);}}
    

    With line breaks and tabs

    class a{
    
        public static void main(String[]a){
            String b="";
            for(char c:new java.util.Scanner(System.in).nextLine().toCharArray())b+=c>64&&c<91?(char)(c+32):c;
            System.out.print(b);
        }
    
    }
    

    asteri

    Posted 2013-10-06T13:34:59.013

    Reputation: 824

    1

    See the question owner's recent comment.

    – manatwork – 2013-10-06T15:14:13.850

    manatwork is right. And in the challenge, I also wrote ASCII string, so it would be very unlikely that the input string would contain uppercase characters only. – ProgramFOX – 2013-10-06T15:15:23.327

    Okay, will update when I get home. – asteri – 2013-10-06T16:58:27.320

    @ProgramFOX Updated. – asteri – 2013-10-06T17:26:56.027

    0

    bash: 11 characters?

    $ i="This is A STRING"
    $ echo ${i,,}
    this is a string
    

    devnull

    Posted 2013-10-06T13:34:59.013

    Reputation: 1 591

    2It's a built'in toLower function! – F. Hauri – 2013-12-04T07:55:19.753

    @F.Hauri I assume that you'd need to add this comment on most of the answers in the thread. – devnull – 2013-12-04T07:58:37.100

    @F.Hauri Wrong. Thats not a built-in *function*. That\s a built-in case-modification operator. – hidefromkgb – 2018-09-19T15:57:43.760

    0

    Python - 96 94 82

    for c in input():print(chr(ord(c)-65+97),end="")if c.isupper()else print(c,end="")
    

    Ungolfed version:

    for char in input():
        print(chr(ord(char) - (ord("A") - ord("a"))), end="") if char.isupper() \
        else print(char, end="")
    

    golfer9338

    Posted 2013-10-06T13:34:59.013

    Reputation: 411

    1You can replace ord("A") with 65 and ord("a") with 97 to save 12 bytes. – nyuszika7h – 2014-08-20T10:26:05.850

    0

    Because I felt like it (I know that I'm a bit late)

    Java - 162

    class a{public static void main(String[]a){for(char c:(new java.util.Scanner(System.in)).nextLine().toCharArray())System.out.print((char)((c>64&&c<91)?c+32:c));}}
    

    expanded:

    public class a{
        public static void main(String[] a) {
            for (char c : (new java.util.Scanner(System.in)).nextLine().toCharArray()) {
                System.out.print((char) ((c > 64 && c < 91) ? c + 32 : c));
            }
        }
    }
    

    Justin

    Posted 2013-10-06T13:34:59.013

    Reputation: 19 757

    0

    Java - 119

    class a{static{for(char c:(new java.util.Scanner(System.in)).nextLine().toCharArray())System.out.print((char)(c|32));}}
    

    Works for every character except @ which becomes the back-tick (`), and the following (foo:bar for foo becomes bar) :

    \:|
    ]:}
    ^:~
    _:
    

    Even though this isn't a true solution because it has 5 cases where it doesn't work, I thought it was very interesting. It takes the characters of the input and does a bitwise or with ' ' (c|32).

    I discovered that c^32 swaps the case when the input is only letters while solving a project euler problem. I wondered what would happen in I changed the XOR to an OR.

    Justin

    Posted 2013-10-06T13:34:59.013

    Reputation: 19 757

    0

    Game Maker Language, 228

    Make script/function s with this code, 53 characters:

    a=argument0;b=string_replace_all(b,string_upper(a),a)
    

    Then, use this 175 character code:

    b=get_string('','')s('a')s('b')s('c')s('d')s('e')s('f')s('g')s('h')s('i')s('j')s('k')s('l')s('m')s('n')s('o')s('p')s('q')s('r')s('s')s('t')s('u')s('v')s('w')s('x')s('y')s('z')
    

    The input (stored in variable b), is now lowercase.

    Timtech

    Posted 2013-10-06T13:34:59.013

    Reputation: 12 038

    0

    JavaScript 124

    The closest I got was 124 characters...

    b=prompt();a=[];for(c=b.length;c--;)a[c]="@"<b[c]&"[">b[c]?String.fromCharCode(b[c].charCodeAt(0)+32):b[c];alert(a.join(""))
    

    @Firefly, nice work with using the "map" command... I've yet to master it...

    WallyWest

    Posted 2013-10-06T13:34:59.013

    Reputation: 6 949

    0

    Japt v2.0a0, 7 bytes

    r\A_c^H
    

    Try it

    Shaggy

    Posted 2013-10-06T13:34:59.013

    Reputation: 24 623

    0

    q 37 bytes

    {$[x in .Q.A;x:(.Q.A!.Q.a)[x];x]}each
    

    Explanation

    $[x in .Q.A          // If the character is in the capitals list
      x:(.Q.A!.Q.a)[x]   // Create a key mapping between upper and lower case letters,
                         // and set x to it's pair
      x]}                // else, just return the character
      each               // apply to each character in string passed in
    

    Example

    q){$[x in .Q.A;x:(.Q.A!.Q.a)[x];x]}each "LoWersWFSfdgSA"
    "lowerswfsfdgsa"
    

    Thaufeki

    Posted 2013-10-06T13:34:59.013

    Reputation: 421

    0

    JavaScript, 58 bytes

    s=>s.replace(/[A-Z]/g,x=>(x.charCodeAt()-55).toString(36))
    

    12Me21

    Posted 2013-10-06T13:34:59.013

    Reputation: 6 110

    0

    Forth (gforth), 55 bytes

    : f 0 do dup i + c@ dup 65 91 within 32 * - emit loop ;
    

    Try it online!

    Explanation

    Iterates through the string, for each character:

    • Gets the ascii value of the character
    • If between 65 and 90 (inclusive) add 32, else leave as is
    • Print the char corresponding to that value

    Code Explanation

    : f                   \ start new word definition
      0 do                \ start counted loop from 0 to string-length - 1
        dup i +           \ duplicate the string address and add the loop index
        c@                \ get the ascii char value at that address
        dup 65 91 within  \ check if value is between 65 and 90 (-1 = true, 0 = false)
        32 * -            \ multiply result by 32 and subtract from original number
        emit              \ output value
      loop                \ end loop
    ;                     \ end word definition
    

    reffu

    Posted 2013-10-06T13:34:59.013

    Reputation: 1 361

    0

    ><>, 17 bytes

    i::" @["{)${(**+o
    

    Try it online!

    Emigna

    Posted 2013-10-06T13:34:59.013

    Reputation: 50 798

    0

    J, 39 bytes

    ((<&91*64&<)(32+#)`(I.@[)`]}])&.(a.i.])
    

    Try it online!

    Convert the string to indexes within the J alphabet a., create a mask for those that are uppercase (<&91*64&<), then use amend } to update just those indexes I. that are uppercase with their lowercase counterpart (32+#)

    Jonah

    Posted 2013-10-06T13:34:59.013

    Reputation: 8 729

    0

    JavaScript(ES6), 92/90 bytes

    prompt().split('').map(x=>String.fromCharCode((y=x.charCodeAt(),y>64&y<91)?y+32:y)).join('')
    
    l=a=>a.split('').map(x=>String.fromCharCode((y=x.charCodeAt(),y>64&y<91)?y+32:y)).join('')
    

    Turn the input string into a char array, turn all uppercase letters into lowercase, and turn it back into a string.(Difference between uppercase letter and lowercase letter('a'-'A') is 32, 64 is '@', 91 is '['.)

    If using alerts is required, that increases the characters by 7, which means it's 99 bytes.

    Rouli Freman

    Posted 2013-10-06T13:34:59.013

    Reputation: 1

    0

    Powershell - 81 characters

    Program:

    param($s);[char[]]$s|%{if($_-lt90-and$_-gt64){$n+=[char](+$_+32)}else{$n+=$_}};$n
    

    Example Usage:

    .\lower.ps1 -s "TEST!"
    

    Output:

    test!
    

    How it works:

    It just adds 32 to the decimal value of the ASCII character, which is the lowercase version.
    It only does this if it is within 64-90, which is all capital letters.

    Vasili Syrakis

    Posted 2013-10-06T13:34:59.013

    Reputation: 181

    I don't know where you got 63 from. That's 81 characters. http://mothereff.in/byte-counter

    – nyuszika7h – 2014-08-20T10:16:43.580

    0

    Delphi XE3 (153 chars)

    uses System.SysUtils;var u:string;i:int8;begin readln(u);for i:=1to Length(u)do if CharInSet(u[i],['A'..'Z'])then u[i]:=Chr(Ord(u[i])+32);writeln(u);end.
    

    Not a winner but fun to do :)

    with indent

    uses
      System.SysUtils;
    var
      u:string;
      i:int8;
    begin
      readln(u);
      for i:=1to Length(u)do
        if CharInSet(u[i],['A'..'Z'])then
          u[i]:=Chr(Ord(u[i])+32);
        writeln(u);
    end.
    

    Teun Pronk

    Posted 2013-10-06T13:34:59.013

    Reputation: 2 599

    0

    J 35

    echo(+32*64&<*.95&>)&.(a.&i.)1!:1]3
    

    Using the magical under &.. This looks up the ascii code, then adds 32 * the boolean inRange , where inRange is x>64 and x<91. The under operation automatically applies the inverse lookup afterwards, resulting in the wanted lowercase.

    In action:

       echo(+32*64&<*.91&>)&.(a.&i.)1!:1]3
    Hello World AZ!
    hello world az!
    

    jpjacobs

    Posted 2013-10-06T13:34:59.013

    Reputation: 3 440

    0

    Python 3, 78 bytes

    t=input()
    for c in t:
     x=ord(c)
     if 64<x<91:t=t.replace(c,chr(x+32))
    print(t)
    

    Argenis García

    Posted 2013-10-06T13:34:59.013

    Reputation: 223