String concatenation: Difference between revisions
(→{{header|C}}: demonstrate classic string functions) |
(fortran+php) |
||
Line 59: | Line 59: | ||
s" there!" pad +place \ +place is called "append" on some Forths |
s" there!" pad +place \ +place is called "append" on some Forths |
||
pad count type |
pad count type |
||
=={{header|Fortran}}== |
|||
<pre>program StringConcatenation |
|||
integer, parameter :: maxstringlength = 64 |
|||
character (*), parameter :: s = "hello" |
|||
character (maxstringlength) :: s1 |
|||
print *,s // " literal" |
|||
s1 = s // " literal" |
|||
print *,s1 |
|||
end program</pre> |
|||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
Line 116: | Line 129: | ||
my $s1 = $s . " literal"; |
my $s1 = $s . " literal"; |
||
print $s1 . "\n";</perl> |
print $s1 . "\n";</perl> |
||
=={{header|PHP}}== |
|||
<php><?php |
|||
$s = "hello"; |
|||
echo $s . " literal" . "\n"; |
|||
$s1 = $s . " literal"; |
|||
echo $s1 . "\n"; |
|||
?></php> |
|||
=={{header|UNIX Shell}}== |
=={{header|UNIX Shell}}== |
Revision as of 00:48, 18 December 2008
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.
BASIC
<qbasic>s$ = "hello" print s$;" literal" 'or s$ + " literal" s2$ = s$ + " literal" print s2$</qbasic> Output:
hello literal hello literal
C
<c>#include <stdio.h>
- include <stdlib.h>
- 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);
}</c>
C++
<cpp>#include <string>
- 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;
}</cpp> Output:
hello literal hello literal
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
<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); }
}</java> Output:
hello literal hello literal
JavaScript
<javascript>var s = "hello" print(s + " there!") </javascript>
Logo
make "s "hello print word :s "| there!|
Objective-C
<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;
}</objc>
Perl
<perl>#! /usr/bin/perl my $s = "hello"; print $s . " literal" . "\n"; my $s1 = $s . " literal"; print $s1 . "\n";</perl>
PHP
<php><?php $s = "hello"; echo $s . " literal" . "\n"; $s1 = $s . " literal"; echo $s1 . "\n"; ?></php>
UNIX Shell
s="hello" echo "$s literal" s1="$s literal" echo $s1