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\$
  • 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

49 Answers 49

1
2
1
\$\begingroup\$

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
| improve this answer | |
\$\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);
    }

}
| improve this answer | |
\$\endgroup\$
  • 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
| improve this answer | |
\$\endgroup\$
  • 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.

| improve this answer | |
\$\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...

| improve this answer | |
\$\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));
        }
    }
}
| improve this answer | |
\$\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.

| improve this answer | |
\$\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.
| improve this answer | |
\$\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!
| improve this answer | |
\$\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="")
| improve this answer | |
\$\endgroup\$
  • 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.

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

Japt v2.0a0, 7 bytes

r\A_c^H

Try it

| improve this answer | |
\$\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"
| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ {x^.Q.a .Q.A?x} for 15 bytes. \$\endgroup\$ – streetster Apr 27 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 at 0:48
0
\$\begingroup\$

JavaScript, 58 bytes

s=>s.replace(/[A-Z]/g,x=>(x.charCodeAt()-55).toString(36))
| improve this answer | |
\$\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
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

><>, 17 bytes

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

Try it online!

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

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+#)

| improve this answer | |
\$\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.

| improve this answer | |
\$\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.