Case conversion

From CodeCodex

Implementation[edit]

Assembly[edit]

mov edx,offset string ; string source goes into edx somehow
upr_loop:
mov al,BYTE PTR [edx]
cmp al,97
jl upr_pool
cmp al,122
jg upr_pool
sub BYTE PTR [edx],32
upr_pool:
add edx,1
or al,al
jnz upr_loop

BASIC[edit]

a$ = "HELLO world"
b$ = "HELLO world"

'Convert a string to lowercase
LET temp$ = ""
FOR x = 1 to LEN(a$)
    LET c = ASC(MID$(a$,x,1))
    IF c > 64 AND c < 91 THEN LET c = c + 32
    LET temp$ = temp$ + CHR$(c)
NEXT x
LET a$ = temp$

'Convert a string to uppercase
LET temp$ = ""
FOR x = 1 to LEN(b$)
    LET c = ASC(MID$(b$,x,1))
    IF c > 96 AND c < 123 THEN LET c = c - 32
    LET temp$ = temp$ + CHR$(c)
NEXT x
LET b$ = temp$

C[edit]

#include <ctype.h>

/*
 * Convert a string to lowercase
 */
char *lc(char *str)
{
   char *t;

   for (t = str; *t; t++)
   {
      /* Convert to lowercase */
      (*t) = tolower(*t);
   }

   /* Done */
   return (str);
}

/*
 * Convert a string to uppercase
 */
char *uc(char *str)
{
   char *t;

   for (t = str; *t; t++)
   {
      /* Convert to uppercase */
      (*t) = toupper(*t);
   }

   /* Done */
   return (str);
}

C++[edit]

#include <algorithm>
#include <string>

using namespace std;

int main() {
    string value("HELLO world");
    transform(value.begin(), value.end(), value.begin(), ::toupper);
}

ColdFusion[edit]

ColdFusion has the built-in Functions Ucase(string) and Lcase(string).

Erlang[edit]

Erlang strings module has the builtin Functions to_lower and to_upper

1> string:to_lower("Hello, World!").
"hello, world!"
2> string:to_upper("Hello, World!").     
"HELLO, WORLD!"

Go[edit]

Go's strings module has ToUpper and ToLower functions:

package main
import (
  "fmt"
  "strings"
)
func main() {
  fmt.Println(strings.ToUpper("éloïse"))
}

Haskell[edit]

Char.toLower and Char.toUpper converts a character to lowercase and uppercase, respectively. Since Strings in Haskell are just lists of Chars, we can just use "map" to operate on a whole string.

Prelude> map Char.toUpper "HELLO world"
"HELLO WORLD"

Java[edit]

Java's built in String class has methods called toUpperCase and toLowerCase.

OCaml[edit]

String.lowercase and String.uppercase. The standard library assumes a latin1 (iso 8859-1) encoding and will do the conversions of accented letters accordingly:

# print_endline (String.uppercase "éloïse");;
ÉLOÏSE
- : unit = ()

Perl[edit]

Perl has builtin functions called lc, lcfirst, uc, and ucfirst. Unlike the majority of other languages, they properly follow locale conventions and employ Unicode case equivalent mapping.

 use utf8; # specify encoding of source, we can use literal characters now
 
 uc 'i'; # I - nothing fancy
 uc 'ö'; # Ö - not in ASCII, but still nothing fancy
 uc 'ĕ'; # Ĕ - some languages start puking because ĕ is not in ISO Latin-1
 uc 'б'; # Б - non-latin script handled just fine
 uc 'ß'; # SS - Unicode case equivalent mapping
 
 # under Turkish locale
 uc 'i'; # İ
 lc 'İ'; # i
 uc 'ı'; # I
 lc 'I'; # ı

PHP[edit]

PHP has builtin functions called strtolower, strtoupper, ucfirst and ucwords

Python[edit]

Python has the builtin String Methods lower, swapcase, capitalize , title and upper.

If using non-latin characters, use unicode('STRiNG').lower(), etc..

Rexx[edit]

The Rexx translate function is quite powerful--it takes four arguments in general, a string to be translated, an output table, an input table, and a pad character. When called with one argument, it returns the uppercase equivalent of its input. Going to lowercase is a bit uglier.


/* Translate string to uppercase */
string = translate(string)

/* Translate to lowercase */
string = translate(string, 'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ')

Ruby[edit]

Ruby has the builtin String Methods downcase, upcase, swapcase and capitalize. Each of these methods also has a in-place modifying version, with an exclamation mark at the end of the name (i.e. downcase!).

Tcl[edit]

string tolower "ABCDEF"
string toupper "abcdef"