27
\$\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\$ – John Dvorak Oct 6 '13 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 '13 at 13:45
  • 1
    \$\begingroup\$ Are regular expressions allowed? Only GolfScript could beat s/./\L\0/g. \$\endgroup\$ – manatwork Oct 6 '13 at 13:49
  • 3
    \$\begingroup\$ @manatwork: surely \L is built in? \$\endgroup\$ – marinus Oct 6 '13 at 13:50
  • \$\begingroup\$ @manatwork: Yes, a regex is allowed. \$\endgroup\$ – ProgramFOX Oct 6 '13 at 13:50

51 Answers 51

1
2
1
\$\begingroup\$

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.

\$\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\$ – Jonah Apr 9 '19 at 16:08
  • 1
    \$\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\$ – 640KB Apr 9 '19 at 16:18
1
\$\begingroup\$

J, 39 30 bytes

(<&91*64&<)`(,:32+])}&.(a.i.])

-9 thanks to xash

Try it online!

Uses Item Amend } to choose which characters need to be lowercased and which passed through unaltered.

It applies this operation "under" conversion to ascii indexes &.(a.i.]) -- it first converts the string to the indexes of its characters, than applies the item amend transformation, then converts back.

The item amend transformation is defined by the gerund (<&91*64&<)`(,:32+]). (,:32+]) defines the two possibilities: the input unaltered, or the the input shifted 32 places to the right, converting A to a, etc. (<&91*64&<) determines which integers go into which transformation category: If an integer is greater than 64 and less than 91 -- ie, the integers corresponding to ABCDEFGHIJKLMNOPQRSTUVWXYZ -- it gets shifted by 32.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Monadic } works here neatly to choose between +32 and unaltered: Try it online! \$\endgroup\$ – xash Dec 31 '20 at 18:41
  • \$\begingroup\$ Very nice. I typically forget about item amend, but occasionally it's the perfect tool. \$\endgroup\$ – Jonah Dec 31 '20 at 18:57
1
\$\begingroup\$

naz, 92 90 bytes

2x1v8a8m2x2v2d3m5s2x3v1x1f1r3x1v4e3x2v2g1o1f0x1x2f3x3v3l1o1f0x1x3f8a8a8a8a1o1f0x1x4f0a0x1f

Works for any null-terminated input string.

Try it online!

Explanation (with 0x instructions removed)

2x1v                   # Set variable 1 equal to 0
8a8m2x2v               # Set variable 2 equal to 64 ("@")
2d3m5s2x3v             # Set variable 3 equal to 91 ("[")
1x1f1r3x1v4e3x2v2g1o1f # Function 1
                       # Read a byte of input
                       # Goto function 4 if it equals variable 1
                       # Goto function 2 if it's greater than variable 2
                       # Otherwise, output it and call the function again
1x2f3x3v3l1o1f         # Function 2
                       # Goto function 3 if the register is less than variable 3
                       # Otherwise, output and jump to function 1
1x3f8a8a8a8a1o1f       # Function 3
                       # Add 32 to the register, output, and call function 1
1x4f0a                 # Function 4
                       # Add 0 to the register
1f                     # Call function 1
\$\endgroup\$
0
\$\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 '13 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 '13 at 15:15
  • \$\begingroup\$ Okay, will update when I get home. \$\endgroup\$ – asteri Oct 6 '13 at 16:58
  • \$\begingroup\$ @ProgramFOX Updated. \$\endgroup\$ – asteri Oct 6 '13 at 17:26
0
\$\begingroup\$

bash: 11 characters?

$ i="This is A STRING"
$ echo ${i,,}
this is a string
\$\endgroup\$
3
  • 2
    \$\begingroup\$ It's a built'in toLower function! \$\endgroup\$ – F. Hauri Dec 4 '13 at 7:55
  • \$\begingroup\$ @F.Hauri I assume that you'd need to add this comment on most of the answers in the thread. \$\endgroup\$ – devnull Dec 4 '13 at 7:58
  • \$\begingroup\$ @F.Hauri Wrong. That`s not a built-in function. That`s a built-in case-modification operator. \$\endgroup\$ – hidefromkgb Sep 19 '18 at 15:57
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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

\$\endgroup\$
0
\$\begingroup\$

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));
        }
    }
}
\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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

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

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="")
\$\endgroup\$
1
  • 1
    \$\begingroup\$ You can replace ord("A") with 65 and ord("a") with 97 to save 12 bytes. \$\endgroup\$ – nyuszika7h Aug 20 '14 at 10:26
0
\$\begingroup\$

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.

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

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

Japt v2.0a0, 7 bytes

r\A_c^H

Try it

\$\endgroup\$
0
\$\begingroup\$

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"
\$\endgroup\$
2
  • 1
    \$\begingroup\$ {x^.Q.a .Q.A?x} for 15 bytes. \$\endgroup\$ – streetster Apr 27 '20 at 11:49
  • \$\begingroup\$ Very nice. never use lookup, was surprised to see it index non-list members as just out-of-bounds (26). \$\endgroup\$ – Thaufeki Apr 28 '20 at 0:48
0
\$\begingroup\$

JavaScript, 58 bytes

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

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

><>, 17 bytes

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

Try it online!

\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

FALSE, 29 bytes

[^$1_=~][$$64>\91>~&[32+]?,]#

Try it online!

\$\endgroup\$
1
2

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.