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

66 Answers 66

56
\$\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\$ Commented Dec 8, 2013 at 12:50
  • \$\begingroup\$ @Timtech I can't count. \$\endgroup\$ Commented Dec 8, 2013 at 23:48
  • 1
    \$\begingroup\$ It's not just unrelated. It's very unrelated. \$\endgroup\$ Commented Aug 20, 2014 at 2:34
  • 1
    \$\begingroup\$ This will have strange results when encountering text that contains the characters ß. \$\endgroup\$ Commented Aug 23, 2014 at 14:26
33
\$\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\$ Commented Oct 6, 2013 at 19:57
  • \$\begingroup\$ Actually that is 11 characters. The -p option is counted as 1. \$\endgroup\$ Commented Oct 8, 2013 at 7:56
  • \$\begingroup\$ @manatwork Or it should be counted as 2 : - and p :) \$\endgroup\$ Commented 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\$ Commented Aug 20, 2014 at 9:46
  • \$\begingroup\$ This could be 9 using y;A-Z;a-z instead! \$\endgroup\$ Commented Apr 24, 2020 at 7:04
21
\$\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\$ Commented 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\$ Commented Oct 10, 2013 at 5:57
  • \$\begingroup\$ Ah, thanks – that makes sense. I’ll keep it in mind! \$\endgroup\$ Commented Oct 10, 2013 at 14:32
9
\$\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\$
9
\$\begingroup\$

Python 3, 48

input().translate({c:c|32for c in range(65,91)}) 
\$\endgroup\$
4
  • \$\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\$ Commented 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\$ Commented Oct 6, 2013 at 18:21
  • 1
    \$\begingroup\$ Your answer follows the spirit of the question, but mine follows the letter of the question... \$\endgroup\$ Commented 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\$ Commented Oct 8, 2013 at 18:56
7
\$\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\$
6
\$\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\$ Commented 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\$ Commented 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\$ Commented 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\$ Commented Oct 6, 2013 at 16:57
  • \$\begingroup\$ (32(23)b.])&.(3&u:), should be 5 bytes shorter. \$\endgroup\$ Commented Oct 20, 2017 at 0:12
6
\$\begingroup\$

C 64 63 59 55 chars

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

žn2ä`‡ 

-1 byte thanks to @Emigna.
-5 bytes thanks to @TheThonnu

Try it online.

Explanation:

žn # Push builtin string "ABC...XYZabc...xyz" 2ä # Split it into two equal-sized parts: ["ABC...XYZ", "abc...xyz"] ` # Pop and push the strings separately to the stack ‡ # Transliterate all uppercase letters to lowercase ones # (after which the result is output implicitly) 
\$\endgroup\$
5
  • 1
    \$\begingroup\$ ÇIS.u32*+çJ saves a byte on your 12-byte version. \$\endgroup\$ Commented 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\$ Commented Apr 9, 2019 at 13:23
  • 1
    \$\begingroup\$ 7 bytes (no case-altering built-ins) \$\endgroup\$ Commented Jun 22, 2023 at 15:15
  • 1
    \$\begingroup\$ @TheThonnu Thanks! And 26ô can actually be . :) ä (split into that many equal sized parts) is kinda the opposite of ô (split into parts of that size). \$\endgroup\$ Commented Jun 22, 2023 at 16:46
  • \$\begingroup\$ Nice: I was looking for that (as I knew it existed), but it's documented as "slice" not "split", so I couldn't find it :( \$\endgroup\$ Commented Jun 22, 2023 at 16:47
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\$ Commented Dec 4, 2013 at 17:46
  • \$\begingroup\$ Amazing trick, @F.Hauri! \$\endgroup\$ Commented Dec 4, 2013 at 18:00
  • \$\begingroup\$ @nyuszika7h, the +1 is the -p command line parameter, not a newline. \$\endgroup\$ Commented Aug 20, 2014 at 12:16
  • \$\begingroup\$ Oh right, sorry. \$\endgroup\$ Commented Aug 20, 2014 at 12:16
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\$ Commented 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\$ Commented 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\$ Commented 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\$ Commented Dec 5, 2013 at 23:55
  • \$\begingroup\$ Using a bitwise and instead of a logical one... nice! \$\endgroup\$ Commented 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\$ Commented 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\$ Commented 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\$ Commented Dec 4, 2013 at 18:04
  • \$\begingroup\$ Doesn't this translate @ to backtick and [\]^_ to {|}~DEL? And therein lies the tricky part.. \$\endgroup\$ Commented Dec 4, 2013 at 18:15
  • 1
    \$\begingroup\$ @FireFly No, $& have to match [A-Z]. \$\endgroup\$ Commented Dec 4, 2013 at 18:19
  • \$\begingroup\$ Oh, my bad. Very cool, then! \$\endgroup\$ Commented 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\$ Commented Oct 7, 2013 at 8:00
  • \$\begingroup\$ How would i go about running it,like this? \$\endgroup\$ Commented Oct 7, 2013 at 8:37
  • \$\begingroup\$ @C5H8NNaO4 str(code here) \$\endgroup\$ Commented 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\$ Commented Oct 7, 2013 at 14:17
  • 1
    \$\begingroup\$ You need a /g flag for this to work properly. \$\endgroup\$ Commented Dec 7, 2013 at 14:36
3
\$\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\$
3
\$\begingroup\$

x86-16 machine code, 15 bytes

Assembled:

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

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, 'a'-'A' ; lowercase the char _STORE: AA STOSB ; store char to [DI], advance DI E2 F2 LOOP _LOOP ; continue loop through string C3 RET ; return to caller 

Callable near function. Input string in DS:[SI], length in CX. Output string in ES:[DI].

Output from PC DOS test program:

enter image description here

\$\endgroup\$
2
  • \$\begingroup\$ where is the 14 byte count coming from? the snippet is longer than that, even without comments... is 14 bytes the compiled program? \$\endgroup\$ Commented Apr 9, 2019 at 16:08
  • 2
    \$\begingroup\$ @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. codegolf.meta.stackexchange.com/a/12340/84624 \$\endgroup\$ Commented Apr 9, 2019 at 16:18
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\$ Commented 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\$ Commented 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\$ Commented 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\$

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\$ Commented 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\$ Commented Oct 13, 2013 at 9:41
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\$ Commented 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\$ Commented Oct 8, 2013 at 13:21
  • \$\begingroup\$ str_ireplace does case insensitive search, which is stretching the rules, if not breaking them. \$\endgroup\$ Commented 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\$ Commented 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\$ Commented 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\$ Commented Dec 4, 2013 at 21:42
  • \$\begingroup\$ @goric Geez, why didn't I think of that already? Still learning, I guess. \$\endgroup\$ Commented 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\$ Commented Sep 20, 2018 at 16:43
  • 1
    \$\begingroup\$ Splat it up for 43 \$\endgroup\$ Commented Jan 24, 2020 at 15:36
2
\$\begingroup\$

Vim, 6 bytes

gUUg~~

Or, if allowed,

Vim, 3 bytes

guu

\$\endgroup\$
1
  • 2
    \$\begingroup\$ ggguG, I'd count that as built-in tho. :^) \$\endgroup\$ Commented Jul 28, 2023 at 6:30
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\$ Commented 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\$ Commented Oct 6, 2013 at 15:15
  • \$\begingroup\$ Okay, will update when I get home. \$\endgroup\$ Commented Oct 6, 2013 at 16:58
  • \$\begingroup\$ @ProgramFOX Updated. \$\endgroup\$ Commented 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\$
1
  • 1
    \$\begingroup\$ c.charCodeAt() -- it defaults to 0 if an index is omitted. Also, breaks on '@' I believe (it gets "lowercased" into backtick) \$\endgroup\$ Commented Oct 7, 2013 at 7:42
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\$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.