同調実験室 - ARGIUS.net

TOP PAGE INDEX Number / 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

05:文字列(2) 結合

機能

  1. 文字列A,B,C,X(空)を設定する
  2. 文字列XにAを連結してXを表示
  3. 文字列XにBを連結してXを表示
  4. 文字列XにCを連結してXを表示

実行結果

$ ./test
Central
Central Processing
Central Processing Unit

$

ソース

C

#include <stdio.h>
#include <string.h>

int main()
{
  char a[8]  = "Central";
  char b[11] = "Processing";
  char c[5]  = "Unit";
  char x[32] = "";
  
  strcat(x, a);
  printf("%s\n", x);
  
  strcat(x, " ");
  strcat(x, b);
  printf("%s\n", x);
  
  strcat(x, " ");
  strcat(x, c);
  printf("%s\n", x);
  
  return 0;
}

C++

#include <iostream>
#include <sstream>

int main()
{
  std::string a,b,c;
  std::string x;
  
  a = "Central";
  b = "Processing";
  c = "Unit";

  x += a;
  std::cout << x << std::endl;
  
  x += " " + b;
  std::cout << x << std::endl;
  
  x += " " + c;
  std::cout << x << std::endl;
  
  return 0;
}

Java

class Main {
    
    public static void main(String[] args) {
        
        final String a = "Central";
        final String b = "Processing";
        final String c = "Unit";
        
        StringBuffer x = new StringBuffer();
        
        x.append(a);
        System.out.println(x);
        
        x.append(" ").append(b);
        System.out.println(x);
            
        x.append(" ").append(c);
        System.out.println(x);
        
    }
  
}

Perl

my $a = "Central";
my $b = "Processing";
my $c = "Unit";
my $x = "";

$x .= $a;
print"$x\n";

$x .= " $b";
print"$x\n";

$x .= " $c";
print"$x\n";

Ruby

a = "Central"
b = "Processing"
c = "Unit"
x = ""

x += a
puts x

x += " " + b
puts x

x += " " + c
puts x

最終更新日 : 2004.08.23

TOP PAGEINDEX Number / 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18