String case
![Task](http://static.miraheze.org/rosettacodewiki/thumb/b/ba/Rcode-button-task-crushed.png/64px-Rcode-button-task-crushed.png)
You are encouraged to solve this task according to the task description, using any language you may know.
Take the string "alphaBETA", and demonstrate how to convert it to UPPER-CASE and lower-case.
4D
$string:="alphaBETA" $uppercase:=Uppercase($string) $lowercase:=Lowercase($string)
ActionScript
var string:String = 'alphaBETA'; var upper:String = string.toUpperCase(); var lower:String = string.toLowerCase();
Ada
<lang ada> with Ada.Characters.Handling; use Ada.Characters.Handling; with Ada.Text_Io; use Ada.Text_Io;
procedure Upper_Case_String is
S : String := "alphaBETA";
begin
Put_Line(To_Upper(S)); Put_Line(To_Lower(S));
end Upper_Case_String; </lang>
AWK
<lang awk>BEGIN {
string = "alphaBETA"; print toupper(a), tolower(a)
}</lang>
ALGOL 68
# Demonstrate toupper and tolower for standard ALGOL 68 strings. This does not work for multibyte character sets. # PROC to upper = ( CHAR c )CHAR: ( IF ABS "a" <= ABS c & ABS c <= ABS "z" THEN REPR ( ABS c - ABS "a" + ABS "A" ) ELSE c FI ); PROC to lower = ( CHAR c )CHAR: ( IF ABS "A" <= ABS c & ABS c <= ABS "Z" THEN REPR ( ABS c - ABS "A" + ABS "a" ) ELSE c FI ); # upper-cases s in place # PROC str to upper = (REF STRING s)VOID : ( FOR i FROM LWB s TO UPB s DO s[i] := toupper(s[i]) OD ); # lower-cases s in place # PROC str to lower = (REF STRING s)VOID : ( FOR i FROM LWB s TO UPB s DO s[i] := tolower(s[i]) OD ); main: ( STRING t := "alphaBETA"; str to upper(t); printf(($"uppercase: "gl$, t)); str to lower(t); printf(($"lowercase: "gl$, t)) )
APL
a←'abcdefghijklmnopqrstuvwxyz' A←'ABCDEFGHIJKLMNOPQRSTUVWXYZ' X←'alphaBETA' (a,⎕AV)[(A,⎕AV)⍳'alphaBETA'] alphabeta (A,⎕AV)[(a,⎕AV)⍳'alphaBETA'] ALPHABETA
BASIC
s$ = "alphaBETA" PRINT UCASE$(s$) PRINT LCASE$(s$)
C
<lang c> // Demonstrate toupper and tolower for // standard C strings. // This does not work for multibyte character sets.
- include <ctype.h>
- include <stdio.h>
//upper-cases s in place void str_toupper(char *s) {
while(*s) { *s=toupper(*s); s++; }
}
//lower-cases s in place
void str_tolower(char *s)
{
while(*s) { *s=tolower(*s); s++; }
}
int main(int argc, char *argv[]) {
char t[255]="alphaBETA"; str_toupper(t); printf("uppercase: %s\n", t); str_tolower(t); printf("lowercase: %s\n", t); return 0;
} </lang>
C++
This method does the transform in-place. Alternate methods might return a new copy or use a stream manipulator.
<lang cpp>
- include <algorithm>
- include <string>
- include <cctype>
/// \brief in-place convert string to upper case /// \return ref to transformed string void str_toupper(std::string &str) {
std::transform(str.begin(), str.end(), str.begin(), (int(*)(int)) std::toupper);
}
/// \brief in-place convert string to lower case /// \return ref to transformed string void str_tolower(std::string &str) {
std::transform(str.begin(), str.end(), str.begin(), (int(*)(int)) std::tolower);
} </lang>
Here is sample usage code:
<lang cpp>
- include <iostream>
- include <string>
using namespace std; int main() {
string foo("_upperCas3Me!!"); str_toupper(foo); cout << foo << endl; str_tolower(foo); cout << foo << endl; return 0;
} </lang>
C#
<lang csharp> string array = "alphaBETA"; System.Console.WriteLine(array.ToUpper()); System.Console.WriteLine(array.ToLower()); </lang>
Clojure
(def string "alphaBETA) (println (. string toUpperCase)) (println (. string toLowerCase))
ColdFusion
converting a string literal
<cfset upper = UCase("alphaBETA")> <cfset lower = LCase("alphaBETA")>
converting the value of a variable
<cfset string = "alphaBETA"> <cfset upper = UCase(string)> <cfset lower = LCase(string)>
Common Lisp
You can use the string-upcase function to perform upper casing:
CL-USER> (string-upcase "alphaBETA") "ALPHABETA"
and you can do lower casing by using string-downcase:
CL-USER> (string-downcase "alphaBETA") "alphabeta"
D
<lang d> import tango.text.Unicode : toUpper, toLower; import tango.io.Stdout;
void main() {
auto str = "alphaBETA"; Stdout(str.toUpper()).newline; Stdout(str.toLower()).newline;
} </lang>
Delphi
writeln(uppercase('alphaBETA')); writeln(lowercase('alphaBETA'));
E
["alphaBETA".toUpperCase(), "alphaBETA".toLowerCase()]
Factor
"alphaBETA" >lower "alphaBETA" >upper
Forth
ANS Forth does not have words to convert case for either strings or characters. For known alpha-numeric ASCII characters, the following can be used:
: tolower ( C -- c ) 32 or ; : toupper ( c -- C ) 32 invert and ; : lower ( addr len -- ) over + swap do i c@ tolower i c! loop ; : upper ( addr len -- ) over + swap do i c@ toupper i c! loop ;
If the character range is unknown, these definitions are better:
: tolower ( C -- c ) dup [char] A [char] Z 1+ within if 32 + then ; : toupper ( c -- C ) dup [char] a [char] z 1+ within if 32 - then ;
create s ," alphaBETA" s count type s count 2dup upper type s count 2dup lower type
Output:
alphaBETA ALPHABETA alphabeta
Fortran
<lang fortran>
program example implicit none character(9) :: teststring = "alphaBETA" call To_upper(teststring) write(*,*) teststring call To_lower(teststring) write(*,*) teststring contains subroutine To_upper(str) character(*), intent(in out) :: str integer :: i do i = 1, len(str) select case(str(i:i)) case("a":"z") str(i:i) = achar(iachar(str(i:i))-32) end select end do end subroutine To_upper subroutine To_lower(str) character(*), intent(in out) :: str integer :: i do i = 1, len(str) select case(str(i:i)) case("A":"Z") str(i:i) = achar(iachar(str(i:i))+32) end select end do end subroutine To_Lower
end program example
</lang>
Haskell
import Data.Char s = "alphaBETA" lower = map toLower s upper = map toUpper s
IDL
str = "alphaBETA" print, str print, strupcase(str) print, strlowcase(str)
J
upper=: (a.,~a.{~65+i.26) {~ (a.,~a.{~97+i.26) i. ] lower=: (a.,~a.{~97+i.26) {~ (a.,~a.{~65+i.26) i. ]
For example:
upper 'alphaBETA' ALPHABETA lower 'alphaBETA' alphabeta
Java
<lang java> String str = "alphaBETA"; System.out.println(str.toUpperCase()); System.out.println(str.toLowerCase()); </lang> You could also easily create a swapCase method using Character.isLowerCase(), Character.isUpperCase(), and Character.isLetter().
JavaScript
<lang javascript> alert( "alphaBETA".toUpperCase() ); alert( "alphaBETA".toLowerCase() ); </lang>
Output:
ALPHABETA alphabeta
<lang javascript> var string = "alphaBETA"; var uppercase = string.toUpperCase(); var lowercase = string.toLowerCase(); </lang>
Logo
print uppercase "alphaBETA ; ALPHABETA print lowercase "alphaBETA ; alphabeta
MAXScript
Requires MAX 2008
str = "alphaBETA" print (toUpper str) print (toLower str)
Nial
toupper 'alphaBETA' =ALPHABETA tolower 'alphaBETA' =alphabeta
Objective-C
<lang objc> NSLog([@"alphaBETA" uppercaseString]); NSLog([@"alphaBETA" lowercaseString]); </lang>
OCaml
<lang ocaml> let _ =
let str = "alphaBETA" in Printf.printf "%s\n" (String.uppercase str); Printf.printf "%s\n" (String.lowercase str);
</lang>
Oz
%% to compile : ozc -x <file> functor import Open Application define Stdout = {New Open.file init(name:stdout)} proc{PrintLn Msg} {Stdout write(vs:Msg#'\n')} end fun{SwapCase Ch} if{Char.isUpper Ch} then {Char.toLower Ch} elseif {Char.isLower Ch} then {Char.toUpper Ch} else Ch end end fun{StrCase CharCaseFunc InStr} {Map InStr CharCaseFunc} end {PrintLn {StrCase Char.toUpper "alphaBETA"}} {PrintLn {StrCase Char.toLower "alphaBETA"}} {PrintLn {StrCase SwapCase "alphaBETA"}} {Application.exit 0} end
OpenEdge/Progress
caps("alphaBETA") lc("alphaBETA")
Perl
<lang perl> my $string = "alphaBETA"; print uc($string), "\n"; print lc($string), "\n"; </lang>
Also works in Perl 4 if the my is removed.
<lang perl> my $string = "alphaBETA"; say $string.uc say $string.lc </lang>
PHP
<lang php> $str = "alphaBETA"; echo strtoupper($str), "\n"; echo strtolower($str), "\n"; </lang>
Pop11
lvars str = 'alphaBETA'; lowertoupper(str) => uppertolower(str) =>
Python
<lang python> s = "alphaBETA" print s.upper() print s.lower() print s.swapcase() </lang>
Raven
'alphaBETA' upper 'alhpaBETA' lower
Ruby
<lang ruby> "alphaBETA".downcase # => "alphabeta" "alphaBETA".upcase # => "ALPHABETA" "alphaBETA".swapcase # => "ALPHAbeta" </lang>
Scheme
<lang scheme> (define s "alphaBETA") (list->string (map char-upcase (string->list s))) (list->string (map char-downcase (string->list s))) </lang>
Seed7
writeln(upper("alphaBETA")); writeln(lower("alphaBETA"));
Smalltalk
'ALPHAbeta' asUppercase. 'ALPHAbeta' asLowercase.
Standard ML
val strupr = String.map Char.toUpper; val strlwr = String.map Char.toLower;
Test
- strupr "alphaBETA"; val it = "ALPHABETA" : string - strlwr "alphaBETA"; val it = "alphabeta" : string
SQL
declare @s varchar(10) set @s = 'alphaBETA' print upper(@s) print lower(@s)
Tcl
set string alphaBETA string toupper $string # ==> ALPHABETA string tolower $string #==> alphabeta
Toka
needs ctype [ i 1 - ] is i [ string.getLength 0 [ dup i + c@ toupper over i + c! ] countedLoop ] is string.toUpper [ string.getLength 0 [ dup i + c@ tolower over i + c! ] countedLoop ] is string.toLower " alphaBETA" string.toUpper type cr " alphaBETA" string.toLower type cr
UnixPipes
echo "alphaBETA" |tr '[a-z]' '[A-Z]' echo "alphaBETA" |tr '[A-Z]' '[a-z]'
VBScript
Dim MyWord MyWord = UCase("Hello World") ' Returns "HELLO WORLD". MyWord = LCase("Hello World") ' Returns "hello world".
Visual Basic .NET
Works with Visual Basic 2008.
' Define 's' Dim s AS String = "alphaBETA" ' Change 's' to Upper Case. s = s.ToUpper() ' Change 's' to Lower Case. s = s.ToLower()
- Programming Tasks
- Text processing
- 4D
- ActionScript
- Ada
- AWK
- ALGOL 68
- APL
- BASIC
- C
- Libc
- C++
- STL
- C sharp
- Clojure
- ColdFusion
- Common Lisp
- D
- Delphi
- E
- Factor
- Forth
- Fortran
- Haskell
- IDL
- J
- Java
- JavaScript
- Logo
- MAXScript
- Nial
- Objective-C
- OCaml
- Oz
- OpenEdge/Progress
- Perl
- PHP
- Pop11
- Python
- Raven
- Ruby
- Scheme
- Seed7
- Smalltalk
- Standard ML
- SQL
- Tcl
- Toka
- UnixPipes
- VBScript
- Visual Basic .NET