Case conversion
From CodeCodex
Contents |
[edit] Assembly
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
[edit] BASIC
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$
[edit] C
#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);
}
[edit] C++
#include <algorithm>
#include <cctype>
#include <iostream>
int main() {
std::string x = "HELLO world";
// map "toupper" to everything from the beginning to the end of "x",
// and output the result back to x itself
std::transform(x.begin(), x.end(), x.begin(), (int (*)(int))std::toupper);
// for some unknown reason my compiler gives type errors unless I put in the cast above
std::cout << x << std::endl; // prints "HELLO WORLD"
return 0;
}
[edit] ColdFusion
ColdFusion has the built-in Functions Ucase(string) and Lcase(string).
[edit] Haskell
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"
[edit] Java
Java's built in String class has methods called toUpperCase and toLowerCase.
[edit] OCaml
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 = ()
[edit] Perl
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'; # ı
[edit] PHP
PHP has builtin functions called strtolower, strtoupper, ucfirst and ucwords
[edit] Python
Python has the builtin String Methods lower, swapcase, capitalize , title and upper.
If using non-latin characters, use unicode('STRiNG').lower(), etc..
[edit] Rexx
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')
[edit] Ruby
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!).

