String case: Difference between revisions

From Rosetta Code
Content added Content deleted
Line 155: Line 155:
==[[Forth]]==
==[[Forth]]==
[[Category:Forth]]
[[Category: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:
{{broken|Forth}}
: 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 ;

'''Interpreter:'''[[Win32Forth]] 4.2


create s ," alphaBETA"
create s ," alphaBETA"

Revision as of 17:50, 29 May 2007

Task
String case
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

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;

C

Standard: C

Compiler: GCC gcc (GCC) 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)

Library: libc

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

C++

Standard: ANSI C++

Compiler: GCC g++ (GCC) 3.4.4 (cygming special)

Library: STL

This method does the transform in-place. Alternate methods might return a new copy or use a stream manipulator.

/// \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(),
                 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(),
                 std::tolower);
}

here is sample usage code:

#include <iostream> 

using namespace std;
int main() {
  string foo("_upperCas3Me!!");
  str_toupper(foo);
  cout << foo << endl;
  str_tolower(foo);
  cout << foo << endl;
  return 0;
}

C#

string array = "alphaBETA";
System.Console.WriteLine(array.ToUpper());
System.Console.WriteLine(array.ToLower());

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

Delphi

Compiler: ALL

 writeln(uppercase('alphaBETA'));
 writeln(lowercase('alphaBETA'));

E

["alphaBETA".toUpperCase(),
 "alphaBETA".toLowerCase()]

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 ;

Interpreter:Win32Forth 4.2

create s ," alphaBETA"
s count type
s count 2dup upper type
s count 2dup lower type

Output:

alphaBETA
ALPHABETA
alphabeta

IDL

str = "alphaBETA"
print, str
print, strupcase(str) 
print, strlowcase(str)

Java

String str = "alphaBETA";
System.out.println(str.toUpperCase());
System.out.println(str.toLowerCase());

JavaScript

alert( "alphaBETA".toUpperCase() );
alert( "alphaBETA".toLowerCase() );

Output:

ALPHABETA
alphabeta

Perl

Interpreter: Perl v5.x

my $string = "alphaBETA";
my $uppercase = uc($string);
my $lowercase = lc($string);

Pop11

lvars str = 'alphaBETA';
lowertoupper(str) =>
uppertolower(str) =>

Python

s = "alphaBETA"
print s.upper()
print s.lower()

Ruby

Interpreter: Ruby 1.8

"alphaBETA".downcase # => "alphabeta"
"alphaBETA".upcase # => "ALPHABETA"
"alphaBETA".swapcase # => "ALPHAbeta"

Seed7

writeln(upper("alphaBETA"));
writeln(lower("alphaBETA"));

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

--tested in Microsoft SQL 2005
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
 [ count [ dup I + c@ toupper over I + c! ] +iterate ] is >upper
 [ count [ dup I + c@ tolower over I + c! ] +iterate ] is >lower
 
 " heLlO wORld" >upper type cr
 " HeLlO wORld" >lower type cr