String concatenation: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
No edit summary
Line 2: Line 2:


=={{header|ActionScript}}==
=={{header|ActionScript}}==
<actionscript>
<lang actionscript>
package
package
{
{
Line 16: Line 16:
}
}
}
}
</lang>
</actionscript>


=={{header|Ada}}==
=={{header|Ada}}==
<ada>
<lang ada>
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Text_IO; use Ada.Text_IO;


Line 32: Line 32:
end;
end;
end String_Concatenation;
end String_Concatenation;
</ada>
</lang>
Sample output:
Sample output:
<pre>
<pre>
Line 57: Line 57:
=={{header|BASIC}}==
=={{header|BASIC}}==
{{works with|QuickBasic|4.5}}
{{works with|QuickBasic|4.5}}
<qbasic>s$ = "hello"
<lang qbasic>s$ = "hello"
print s$;" literal" 'or s$ + " literal"
print s$;" literal" 'or s$ + " literal"
s2$ = s$ + " literal"
s2$ = s$ + " literal"
print s2$</qbasic>
print s2$</lang>
Output:
Output:
<pre>hello literal
<pre>hello literal
Line 66: Line 66:


=={{header|C}}==
=={{header|C}}==
<c>#include <stdio.h>
<lang c>#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <string.h>
#include <string.h>
Line 90: Line 90:
puts(s2);
puts(s2);
free(s2);
free(s2);
}</c>
}</lang>


=={{header|C++}}==
=={{header|C++}}==
<cpp>#include <string>
<lang cpp>#include <string>
#include <iostream>
#include <iostream>


Line 102: Line 102:
std::cout << s2 << std::endl;
std::cout << s2 << std::endl;
return 0;
return 0;
}</cpp>
}</lang>
Output:
Output:
<pre>hello literal
<pre>hello literal
Line 136: Line 136:


=={{header|Java}}==
=={{header|Java}}==
<java>public class Str{
<lang java>public class Str{
public static void main(String[] args){
public static void main(String[] args){
String s = "hello";
String s = "hello";
Line 143: Line 143:
System.out.println(s2);
System.out.println(s2);
}
}
}</java>
}</lang>
Output:
Output:
<pre>hello literal
<pre>hello literal
Line 149: Line 149:


=={{header|JavaScript}}==
=={{header|JavaScript}}==
<javascript>var s = "hello"
<lang javascript>var s = "hello"
print(s + " there!")
print(s + " there!")
</javascript>
</lang>


=={{header|Logo}}==
=={{header|Logo}}==
Line 158: Line 158:


=={{header|Objective-C}}==
=={{header|Objective-C}}==
<objc>#import <Foundation/Foundation.h>
<lang objc>#import <Foundation/Foundation.h>


int main()
int main()
Line 175: Line 175:
[pool release];
[pool release];
return 0;
return 0;
}</objc>
}</lang>


=={{header|OCaml}}==
=={{header|OCaml}}==
<ocaml>let s = "hello"
<lang ocaml>let s = "hello"
let s1 = s ^ " literal"
let s1 = s ^ " literal"
let () =
let () =
print_endline (s ^ " literal");
print_endline (s ^ " literal");
(* or Printf.printf "%s literal\n" s; *)
(* or Printf.printf "%s literal\n" s; *)
print_endline s1</ocaml>
print_endline s1</lang>


=={{header|Pascal}}==
=={{header|Pascal}}==
<pascal>Program StringConcat;
<lang pascal>Program StringConcat;
Var
Var
s, s1 : String;
s, s1 : String;
Line 196: Line 196:
{ s1 := s + ' literal'; works too, with FreePascal }
{ s1 := s + ' literal'; works too, with FreePascal }
writeln(s1);
writeln(s1);
End.</pascal>
End.</lang>


=={{header|Perl}}==
=={{header|Perl}}==
<perl>#! /usr/bin/perl
<lang perl>#! /usr/bin/perl
my $s = "hello";
my $s = "hello";
print $s . " literal" . "\n";
print $s . " literal" . "\n";
my $s1 = $s . " literal";
my $s1 = $s . " literal";
print $s1 . "\n";</perl>
print $s1 . "\n";</lang>


=={{header|PHP}}==
=={{header|PHP}}==
<php><?php
<lang php><?php
$s = "hello";
$s = "hello";
echo $s . " literal" . "\n";
echo $s . " literal" . "\n";
$s1 = $s . " literal";
$s1 = $s . " literal";
echo $s1 . "\n";
echo $s1 . "\n";
?></php>
?></lang>


=={{header|Python}}==
=={{header|Python}}==
<python>s = "hello"
<lang python>s = "hello"
print s + " literal"
print s + " literal"
s1 = s + " literal"
s1 = s + " literal"
print s1</python>
print s1</lang>


=={{header|Ruby}}==
=={{header|Ruby}}==
<ruby>s = "hello"
<lang ruby>s = "hello"
puts s + " literal"
puts s + " literal"
s1 = s + " literal"
s1 = s + " literal"
puts s1</ruby>
puts s1</lang>


=={{header|Scheme}}==
=={{header|Scheme}}==
<scheme>(define s "hello")
<lang scheme>(define s "hello")
(display (string-append s " literal"))
(display (string-append s " literal"))
(newline)
(newline)
(define s1 (string-append s " literal"))
(define s1 (string-append s " literal"))
(display s1)
(display s1)
(newline)</scheme>
(newline)</lang>


=={{header|Smalltalk}}==
=={{header|Smalltalk}}==

Revision as of 15:53, 3 February 2009

Task
String concatenation
You are encouraged to solve this task according to the task description, using any language you may know.

Set a string variable equal to any text value. Print it to the console concatenated with a string literal. Create a new string variable whose value is the other variable concatenated with a string literal. Print this new variable.

ActionScript

<lang actionscript> package {

   public class Str
   {
       public static function main():void
       {
           var s:String = "hello";
           trace(s + " literal");
           var s2:String = s + " literal";
           trace(s2);
       }
   }

} </lang>

Ada

<lang ada> with Ada.Text_IO; use Ada.Text_IO;

procedure String_Concatenation is

  S : String := "Hello";

begin

  Put_Line (S & " literal");
  declare
     S1 : String := S & " literal";
  begin
     Put_Line (S1);
  end;

end String_Concatenation; </lang> Sample output:

Hello literal
Hello literal

AWK

BEGIN {
   s = "hello";
   print s, " literal";
   s1 = s + " literal";
   print s1;
}

ALGOL 68

STRING s := "hello";
print ((s + " literal", new line));
STRING s1 := s + " literal";
print ((s1, new line))

Output:

hello literal
hello literal

BASIC

Works with: QuickBasic version 4.5

<lang qbasic>s$ = "hello" print s$;" literal" 'or s$ + " literal" s2$ = s$ + " literal" print s2$</lang> Output:

hello literal
hello literal

C

<lang c>#include <stdio.h>

  1. include <stdlib.h>
  2. include <string.h>

char *sconcat(const char *s1, const char *s2) {

 char *s0 = malloc(strlen(s1)+strlen(s2)+1);
 strcpy(s0, s1);
 strcat(s0, s2);
 return s0;

}

int main() {

  const char *s = "hello";
  char *s2;
  
  printf("%s literal\n", s);
  /* or */
  printf("%s%s\n", s, " literal");
  
  s2 = sconcat(s, " literal");
  puts(s2);
  free(s2);

}</lang>

C++

<lang cpp>#include <string>

  1. include <iostream>

int main() {

  std::string s = "hello";
  std::cout << s << " literal" << std::endl;
  std::string s2 = s + " literal";
  std::cout << s2 << std::endl;
  return 0;

}</lang> Output:

hello literal
hello literal

Forth

Works with: GNU Forth
s" hello" pad place
pad count type
s"  there!" pad +place    \ +place is called "append" on some Forths
pad count type

Fortran

program StringConcatenation

integer, parameter          :: maxstringlength = 64
character (*), parameter    :: s = "hello"
character (maxstringlength) :: s1

print *,s // " literal"
s1 = s // " literal"
print *,s1

end program

Haskell

import System.IO
s = "hello"
s1 = s ++ " literal"
main = do putStrLn (s ++ " literal")
          putStrLn s1


Java

<lang java>public class Str{

  public static void main(String[] args){
     String s = "hello";
     System.out.println(s + " literal");
     String s2 = s + " literal";
     System.out.println(s2);
  }

}</lang> Output:

hello literal
hello literal

JavaScript

<lang javascript>var s = "hello" print(s + " there!") </lang>

make "s "hello
print word :s "| there!|

Objective-C

<lang objc>#import <Foundation/Foundation.h>

int main() {

 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
 NSString *s = @"hello";
 printf("%s%s\n", [s UTF8String], " literal");
 
 NSString *s2 = [NSString stringWithFormat:@"%@%@", s, @" literal"];
 puts([s2 UTF8String]);
 /* or */
 NSMutableString *s3 = [NSMutableString stringWithString: s];
 [s3 appendString: @" literal"];
 puts([s3 UTF8String]);
 
 [pool release];
 return 0;

}</lang>

OCaml

<lang ocaml>let s = "hello" let s1 = s ^ " literal" let () =

 print_endline (s ^ " literal");
 (* or Printf.printf "%s literal\n" s; *)
 print_endline s1</lang>

Pascal

<lang pascal>Program StringConcat;

 Var
    s, s1   : String;
 

Begin

   s := 'hello';
   writeln(s + ' literal');
   s1 := concat(s, ' literal');
   { s1 := s + ' literal'; works too, with FreePascal }
   writeln(s1);

End.</lang>

Perl

<lang perl>#! /usr/bin/perl my $s = "hello"; print $s . " literal" . "\n"; my $s1 = $s . " literal"; print $s1 . "\n";</lang>

PHP

<lang php><?php $s = "hello"; echo $s . " literal" . "\n"; $s1 = $s . " literal"; echo $s1 . "\n"; ?></lang>

Python

<lang python>s = "hello" print s + " literal" s1 = s + " literal" print s1</lang>

Ruby

<lang ruby>s = "hello" puts s + " literal" s1 = s + " literal" puts s1</lang>

Scheme

<lang scheme>(define s "hello") (display (string-append s " literal")) (newline) (define s1 (string-append s " literal")) (display s1) (newline)</lang>

Smalltalk

|s s1| s := 'hello'.
(s,' literal') printNl.
s1 := s,' literal'.
s1 printNl.

Tcl

set s hello
puts "$s there!"
append s " there!"
puts $s

UNIX Shell

s="hello"
echo "$s literal"
s1="$s literal"
echo $s1