Case conversion

From CodeCodex

Revision as of 17:34, 18 February 2011 by (Talk)


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


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)
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)
LET b$ = temp$


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


#include <algorithm>
#include <string>

using namespace std;

int main() {
    string value("HELLO world");
    transform(value.begin(), value.end(), value.begin(), static_cast<int(*)(int)> (toupper));
    // Note: static_cast is necessary since there exist multiple overloads of "toupper"


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


Go's strings module has ToUpper and ToLower functions: <highlightsyntax> package main import (


) func main() {


} </highlightsyntax>


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"


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


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");;
- : unit = ()


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 has builtin functions called strtolower, strtoupper, ucfirst and ucwords


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

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


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


string tolower "ABCDEF"
string toupper "abcdef"