Difference between revisions of "Case conversion"

From CodeCodex

(C++)
(C)
Line 40: Line 40:
  
 
==C==
 
==C==
<highlightsyntax language="c">
+
<pre class="c">
 
#include <ctype.h>
 
#include <ctype.h>
  
Line 76: Line 76:
 
   return (str);
 
   return (str);
 
}
 
}
</highlightsyntax>
+
</pre>
  
 
==C++==
 
==C++==

Revision as of 00:33, 28 December 2010

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

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$

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

C++

#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

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

Go

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

 "fmt"
 "strings"

) func main() {

 fmt.Println(strings.ToUpper("éloïse"))

} </highlightsyntax>

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"

Java

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

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 = ()

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'; # ı

PHP

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

Python

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

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

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')

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