String case: Difference between revisions

From Rosetta Code
Content added Content deleted
(C++ version)
(Add Seed7 example)
Line 113: Line 113:
my $uppercase = uc($string);
my $uppercase = uc($string);
my $lowercase = lc($string);
my $lowercase = lc($string);



==[[Python]]==
==[[Python]]==
Line 121: Line 120:
print s.upper()
print s.upper()
print s.lower()
print s.lower()

==[[Seed7]]==
[[Category:Seed7]]

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


==[[SQL]]==
==[[SQL]]==
Line 130: Line 135:
print upper(@s)
print upper(@s)
print lower(@s)
print lower(@s)



==[[Tcl]]==
==[[Tcl]]==

Revision as of 10:39, 23 February 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) 

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

C#

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


Forth

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

Output:

alphaBETA
ALPHABETA
alphabeta

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

Python

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

Seed7

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

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