29
\$\begingroup\$

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!

\$\endgroup\$
10
  • 4
    \$\begingroup\$ unfortunately, I'll have to opt-out. I'm not a beginner. \$\endgroup\$ Oct 6, 2013 at 13:42
  • \$\begingroup\$ @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. \$\endgroup\$
    – ProgramFOX
    Oct 6, 2013 at 13:45
  • 1
    \$\begingroup\$ Are regular expressions allowed? Only GolfScript could beat s/./\L\0/g. \$\endgroup\$
    – manatwork
    Oct 6, 2013 at 13:49
  • 3
    \$\begingroup\$ @manatwork: surely \L is built in? \$\endgroup\$
    – marinus
    Oct 6, 2013 at 13:50
  • \$\begingroup\$ @manatwork: Yes, a regex is allowed. \$\endgroup\$
    – ProgramFOX
    Oct 6, 2013 at 13:50

59 Answers 59

55
\$\begingroup\$

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.

\$\endgroup\$
4
  • 1
    \$\begingroup\$ The Python 3 solution is 26 characters. \$\endgroup\$
    – Timtech
    Dec 8, 2013 at 12:50
  • \$\begingroup\$ @Timtech I can't count. \$\endgroup\$
    – user8777
    Dec 8, 2013 at 23:48
  • 1
    \$\begingroup\$ It's not just unrelated. It's very unrelated. \$\endgroup\$ Aug 20, 2014 at 2:34
  • 1
    \$\begingroup\$ This will have strange results when encountering text that contains the characters ß. \$\endgroup\$
    – FUZxxl
    Aug 23, 2014 at 14:26
34
\$\begingroup\$

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 !
\$\endgroup\$
5
  • 3
    \$\begingroup\$ +1, for the only real-life language that beat out all the less(?) real ones. \$\endgroup\$
    – Behrooz
    Oct 6, 2013 at 19:57
  • \$\begingroup\$ Actually that is 11 characters. The -p option is counted as 1. \$\endgroup\$
    – manatwork
    Oct 8, 2013 at 7:56
  • \$\begingroup\$ @manatwork Or it should be counted as 2 : - and p :) \$\endgroup\$
    – Gowtham
    Oct 8, 2013 at 13:15
  • \$\begingroup\$ 1 if you assume -e (perl -e -> perl -pe), 3 if you assume a script (perl -> perl -p). \$\endgroup\$
    – nyuszika7h
    Aug 20, 2014 at 9:46
  • \$\begingroup\$ This could be 9 using y;A-Z;a-z instead! \$\endgroup\$ Apr 24, 2020 at 7:04
22
\$\begingroup\$

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! @
\$\endgroup\$
3
  • \$\begingroup\$ What makes this the accepted answer, out of curiosity? Gowtham had a 10-character solution first… \$\endgroup\$
    – Ry-
    Oct 10, 2013 at 1:25
  • 1
    \$\begingroup\$ 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.. \$\endgroup\$
    – FireFly
    Oct 10, 2013 at 5:57
  • \$\begingroup\$ Ah, thanks – that makes sense. I’ll keep it in mind! \$\endgroup\$
    – Ry-
    Oct 10, 2013 at 14:32
10
\$\begingroup\$

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 @[`{
\$\endgroup\$
10
\$\begingroup\$

Python 3, 48

input().translate({c:c|32for c in range(65,91)})
\$\endgroup\$
5
  • \$\begingroup\$ 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. \$\endgroup\$
    – asteri
    Oct 6, 2013 at 17:34
  • 1
    \$\begingroup\$ @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()). \$\endgroup\$
    – Ry-
    Oct 6, 2013 at 18:21
  • \$\begingroup\$ Got it. Thanks for the explanation! \$\endgroup\$
    – asteri
    Oct 6, 2013 at 18:25
  • 1
    \$\begingroup\$ Your answer follows the spirit of the question, but mine follows the letter of the question... \$\endgroup\$
    – user8777
    Oct 7, 2013 at 5:55
  • \$\begingroup\$ 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. \$\endgroup\$ Oct 8, 2013 at 18:56
8
\$\begingroup\$

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!
\$\endgroup\$
7
\$\begingroup\$

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.

\$\endgroup\$
6
  • 1
    \$\begingroup\$ Nice! 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) \$\endgroup\$
    – FireFly
    Oct 6, 2013 at 16:33
  • \$\begingroup\$ 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 :) \$\endgroup\$
    – Dan Bron
    Oct 6, 2013 at 16:45
  • \$\begingroup\$ 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. :-) \$\endgroup\$
    – FireFly
    Oct 6, 2013 at 16:49
  • \$\begingroup\$ 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'. \$\endgroup\$
    – Dan Bron
    Oct 6, 2013 at 16:57
  • \$\begingroup\$ (32(23)b.])&.(3&u:), should be 5 bytes shorter. \$\endgroup\$
    – FrownyFrog
    Oct 20, 2017 at 0:12
7
\$\begingroup\$

C 64 63 59 55 chars

main(c){while(c=getchar(),~c)putchar(c-65u<27?c+32:c);}
\$\endgroup\$
7
  • \$\begingroup\$ I count only 63 characters there. \$\endgroup\$
    – manatwork
    Oct 7, 2013 at 9:26
  • \$\begingroup\$ You can lose 9 characters: drop int and ,c>=0. They're not necessary here. \$\endgroup\$
    – JoeFish
    Oct 7, 2013 at 18:00
  • \$\begingroup\$ we need c>=0 as getchar(EOF) will be < 0. Thanks for other suggestion. \$\endgroup\$
    – Rozuur
    Oct 7, 2013 at 21:20
  • 2
    \$\begingroup\$ 1. ~(c=getchar()) 2. c-64u<27 \$\endgroup\$
    – ugoren
    Oct 8, 2013 at 8:28
  • 1
    \$\begingroup\$ Insignificantly small bug: seems there should be 65 instead of 64. pastebin.com/Zc9zMx2W \$\endgroup\$
    – manatwork
    Oct 9, 2013 at 16:03
5
\$\begingroup\$

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
\$\endgroup\$
4
\$\begingroup\$

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 !
\$\endgroup\$
4
  • \$\begingroup\$ Hem, why chr ord ? I'm pretty sure you won't learn anything in reading my answer ;-) \$\endgroup\$
    – F. Hauri
    Dec 4, 2013 at 17:46
  • \$\begingroup\$ Amazing trick, @F.Hauri! \$\endgroup\$
    – manatwork
    Dec 4, 2013 at 18:00
  • \$\begingroup\$ @nyuszika7h, the +1 is the -p command line parameter, not a newline. \$\endgroup\$
    – manatwork
    Aug 20, 2014 at 12:16
  • \$\begingroup\$ Oh right, sorry. \$\endgroup\$
    – nyuszika7h
    Aug 20, 2014 at 12:16
3
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ +1 That is indeed not a Python built-in you are using. Only works on linux, but still very rule-bendy!!! \$\endgroup\$
    – user8777
    Oct 9, 2013 at 3:31
  • \$\begingroup\$ @LegoStormtroopr Works everywhere there is a tr command (which does the right thing) on the path of the invoked shell, I suppose. \$\endgroup\$ Oct 13, 2013 at 9:41
3
\$\begingroup\$

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;
\$\endgroup\$
2
  • 3
    \$\begingroup\$ This is not golf. Don't you feel this piece is very different compared to others ? \$\endgroup\$
    – Ray
    Oct 8, 2013 at 16:57
  • 1
    \$\begingroup\$ @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. \$\endgroup\$
    – Teun Pronk
    Feb 27, 2014 at 7:59
3
\$\begingroup\$

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(""))
\$\endgroup\$
5
  • \$\begingroup\$ 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('')) \$\endgroup\$
    – some
    Dec 5, 2013 at 13:29
  • \$\begingroup\$ @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. \$\endgroup\$
    – FireFly
    Dec 5, 2013 at 23:55
  • \$\begingroup\$ Using a bitwise and instead of a logical one... nice! \$\endgroup\$
    – some
    Dec 6, 2013 at 0:10
  • \$\begingroup\$ An even more ES6 solution (79): L=s=>[String.fromCharCode(c.charCodeAt()|(c>"@"&c<"[")*32)for(c of s)].join(''). Usage: L('SoMeTeXt') \$\endgroup\$
    – Florent
    Feb 28, 2014 at 15:07
  • \$\begingroup\$ 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. \$\endgroup\$
    – FireFly
    Feb 28, 2014 at 17:13
3
\$\begingroup\$

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? 
\$\endgroup\$
4
  • \$\begingroup\$ I forgot +1 for -p sorry @manatwork \$\endgroup\$
    – F. Hauri
    Dec 4, 2013 at 18:04
  • \$\begingroup\$ Doesn't this translate @ to backtick and [\]^_ to {|}~DEL? And therein lies the tricky part.. \$\endgroup\$
    – FireFly
    Dec 4, 2013 at 18:15
  • 1
    \$\begingroup\$ @FireFly No, $& have to match [A-Z]. \$\endgroup\$
    – F. Hauri
    Dec 4, 2013 at 18:19
  • \$\begingroup\$ Oh, my bad. Very cool, then! \$\endgroup\$
    – FireFly
    Dec 4, 2013 at 20:56
3
\$\begingroup\$

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

\$\endgroup\$
13
  • \$\begingroup\$ 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. \$\endgroup\$
    – FireFly
    Oct 7, 2013 at 8:00
  • \$\begingroup\$ How would i go about running it,like this? \$\endgroup\$
    – C5H8NNaO4
    Oct 7, 2013 at 8:37
  • \$\begingroup\$ @C5H8NNaO4 str(code here) \$\endgroup\$ Oct 7, 2013 at 10:11
  • 6
    \$\begingroup\$ I 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. \$\endgroup\$
    – FireFly
    Oct 7, 2013 at 14:17
  • 1
    \$\begingroup\$ You need a /g flag for this to work properly. \$\endgroup\$
    – Ry-
    Dec 7, 2013 at 14:36
3
\$\begingroup\$

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)
\$\endgroup\$
2
  • 1
    \$\begingroup\$ ÇIS.u32*+çJ saves a byte on your 12-byte version. \$\endgroup\$
    – Emigna
    Apr 9, 2019 at 13:17
  • \$\begingroup\$ @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 ç.. \$\endgroup\$ Apr 9, 2019 at 13:23
2
\$\begingroup\$

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))
\$\endgroup\$
3
  • \$\begingroup\$ That's 86 characters - newlines count as 2 characters. (string-functions.com/length.aspx) \$\endgroup\$
    – Timtech
    Dec 8, 2013 at 0:42
  • \$\begingroup\$ @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)) \$\endgroup\$
    – plannapus
    Dec 9, 2013 at 8:26
  • \$\begingroup\$ 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). \$\endgroup\$
    – Timtech
    Dec 9, 2013 at 11:49
2
\$\begingroup\$

Q, 18

.....

{x^ssr/[x]..Q`a`A}
\$\endgroup\$
2
\$\begingroup\$

Q (16)

.......

{x^(.Q.A!.Q.a)x}
\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
4
  • \$\begingroup\$ If you follow Gowtham's Peal solution, you would only count 42 characters. \$\endgroup\$
    – eisberg
    Oct 8, 2013 at 12:04
  • 1
    \$\begingroup\$ @eisberg: Updated the score, leaving a 43-character version in the history in case of any dispute. \$\endgroup\$ Oct 8, 2013 at 13:21
  • \$\begingroup\$ str_ireplace does case insensitive search, which is stretching the rules, if not breaking them. \$\endgroup\$
    – ugoren
    Oct 8, 2013 at 13:55
  • \$\begingroup\$ @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. \$\endgroup\$
    – eisberg
    Oct 9, 2013 at 13:24
2
\$\begingroup\$

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

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Haven't checked on how to get around that [regex]::Replace, but you can save 4 chars by changing [int] to + \$\endgroup\$
    – goric
    Dec 4, 2013 at 21:19
  • 1
    \$\begingroup\$ Actually, 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 \$\endgroup\$
    – goric
    Dec 4, 2013 at 21:42
  • \$\begingroup\$ @goric Geez, why didn't I think of that already? Still learning, I guess. \$\endgroup\$
    – Iszi
    Dec 4, 2013 at 21:53
2
\$\begingroup\$

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
\$\endgroup\$
2
\$\begingroup\$

PowerShell, 53 49 43 bytes

-4 bytes thanks @AdmBorkBork

-6 bytes thanks @Veskah

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

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ cool! ¯\_(ツ)_/¯ \$\endgroup\$
    – mazzy
    Sep 20, 2018 at 16:43
  • 1
    \$\begingroup\$ Splat it up for 43 \$\endgroup\$
    – Veskah
    Jan 24, 2020 at 15:36
2
\$\begingroup\$

K (ngn/k), 19 17 bytes

{`c$x+32*~"A["'x}

Try it online!

Avoids using the floor builtin (_:).

  • ~"A["' generate a bit mask with 1's in the positions in the input containing uppercase letters
  • 32* multiply by 32, which calculates the offsets between uppercase and lowercase ASCII character codes
  • x+ add the offsets to the input (converting the string input to integers)
  • `c$ convert back to characters
\$\endgroup\$
1
\$\begingroup\$

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

}
\$\endgroup\$
4
  • 1
    \$\begingroup\$ See the question owner's recent comment. \$\endgroup\$
    – manatwork
    Oct 6, 2013 at 15:14
  • \$\begingroup\$ 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. \$\endgroup\$
    – ProgramFOX
    Oct 6, 2013 at 15:15
  • \$\begingroup\$ Okay, will update when I get home. \$\endgroup\$
    – asteri
    Oct 6, 2013 at 16:58
  • \$\begingroup\$ @ProgramFOX Updated. \$\endgroup\$
    – asteri
    Oct 6, 2013 at 17:26
1
\$\begingroup\$

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

\$\endgroup\$
2
  • 1
    \$\begingroup\$ c.charCodeAt() -- it defaults to 0 if an index is omitted. Also, breaks on '@' I believe (it gets "lowercased" into backtick) \$\endgroup\$
    – FireFly
    Oct 7, 2013 at 7:42
  • \$\begingroup\$ @FireFly Nice, Thanks!, ok i'll gonna fix it =) \$\endgroup\$
    – C5H8NNaO4
    Oct 7, 2013 at 7:44
1
\$\begingroup\$

Ruby: 66

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

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

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

Haskell - 58

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

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()))
\$\endgroup\$
5
  • \$\begingroup\$ I'm sorry, I had to say that you're not allowed to use a to-lower function on one character. Question updated. \$\endgroup\$
    – ProgramFOX
    Oct 6, 2013 at 15:19
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – ProgramFOX
    Oct 6, 2013 at 15:22
  • \$\begingroup\$ Okay, will update when I get home \$\endgroup\$
    – asteri
    Oct 6, 2013 at 16:58
  • \$\begingroup\$ @ProgramFOX Updated. \$\endgroup\$
    – asteri
    Oct 6, 2013 at 17:34
  • \$\begingroup\$ 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. \$\endgroup\$
    – user8777
    Oct 9, 2013 at 3:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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