さまざまな文字列操作

明解C言語 入門編 > 11. 文字列とポインタ >

さまざまな文字列操作

C


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

int main(int argc, char* argv[])
{
/* strcat */
char st1[100] = "1234567890";
char st2[100] = "ABCDE";

printf("%s + ", st1);
printf("%s = ", st2);
strcat(st1, st2);
printf("%s\n", st1);

char st3[100] = "1234567890";
printf("%s + ", st3);
printf("%s = ", st2);
strncat(st3, st2, 4);
printf("%s\n", st3);

char st4[100] = "1234567890";
printf("%s + ", st4);
printf("%s = ", st2);
strncat(st4, st2, 6);
printf("%s\n", st4);
puts("");

/* strrev */
printf("%s\n", st4);
printf("%s\n", strrev(st4));
puts("");

/* strcmp */
char* pt1 = "123";
char* pt2 = "123";

int i = strcmp(pt1, pt2);
if (i == 0)
printf("%s = %s (%d)\n", pt1, pt2, i);
else if (i > 0)
printf("%s > %s (%d)\n", pt1, pt2, i);
else
printf("%s < %s (%d)\n", pt1, pt2, i);

pt2 = "122";
i = strcmp(pt1, pt2);
if (i == 0)
printf("%s = %s (%d)\n", pt1, pt2, i);
else if (i > 0)
printf("%s > %s (%d)\n", pt1, pt2, i);
else
printf("%s < %s (%d)\n", pt1, pt2, i);

pt2 = "124";
i = strcmp(pt1, pt2);
if (i == 0)
printf("%s = %s (%d)\n", pt1, pt2, i);
else if (i > 0)
printf("%s > %s (%d)\n", pt1, pt2, i);
else
printf("%s < %s (%d)\n", pt1, pt2, i);

pt2 = "999";
i = strcmp(pt1, pt2);
if (i == 0)
printf("%s = %s (%d)\n", pt1, pt2, i);
else if (i > 0)
printf("%s > %s (%d)\n", pt1, pt2, i);
else
printf("%s < %s (%d)\n", pt1, pt2, i);

pt2 = "12";
i = strcmp(pt1, pt2);
if (i == 0)
printf("%s = %s (%d)\n", pt1, pt2, i);
else if (i > 0)
printf("%s > %s (%d)\n", pt1, pt2, i);
else
printf("%s < %s (%d)\n", pt1, pt2, i);

pt2 = "1234";
i = strcmp(pt1, pt2);
if (i == 0)
printf("%s = %s (%d)\n", pt1, pt2, i);
else if (i > 0)
printf("%s > %s (%d)\n", pt1, pt2, i);
else
printf("%s < %s (%d)\n", pt1, pt2, i);

puts("");

/* stricmp */
pt1 = "ABCD";
pt2 = "abcd";
i = strcmp(pt1, pt2);
if (i == 0)
printf("%s = %s (%d)\n", pt1, pt2, i);
else if (i > 0)
printf("%s > %s (%d)\n", pt1, pt2, i);
else
printf("%s < %s (%d)\n", pt1, pt2, i);

i = stricmp(pt1, pt2);
if (i == 0)
printf("%s = %s (%d)\n", pt1, pt2, i);
else if (i > 0)
printf("%s > %s (%d)\n", pt1, pt2, i);
else
printf("%s < %s (%d)\n", pt1, pt2, i);

puts("");

/* strstr */
pt1 = "1234567890";
pt2 = "45";
char* pt3 = strstr(pt1, pt2);
printf("strstr(\"%s\", \"%s\") = %u\n", pt1, pt2, pt3 - pt1);

pt2 = "12";
pt3 = strstr(pt1, pt2);
printf("strstr(\"%s\", \"%s\") = %u\n", pt1, pt2, pt3 - pt1);

pt2 = "123456780";
pt3 = strstr(pt1, pt2);
if (pt3 != '\0')
printf("strstr(\"%s\", \"%s\") = %u\n", pt1, pt2, pt3 - pt1);
else
printf("strstr(\"%s\", \"%s\") = NULL\n", pt1, pt2);

puts("");

/* strchr */
pt1 = "1294567890";
char c = '9';
pt3 = strchr(pt1, c);
printf("strchr(\"%s\", \"%c\") = %u\n", pt1, c, pt3 - pt1);

c = 'A';
pt3 = strchr(pt1, c);
if (pt3 != '\0')
printf("strchr(\"%s\", \"%c\") = %u\n", pt1, c, pt3 - pt1);
else
printf("strchr(\"%s\", \"%c\") = NULL\n", pt1, c);

puts("");

/* strrchr */
c = '9';
pt3 = strrchr(pt1, c);
printf("strrchr(\"%s\", \"%c\") = %u\n", pt1, c, pt3 - pt1);

c = 'A';
pt3 = strrchr(pt1, c);
if (pt3 != '\0')
printf("strrchr(\"%s\", \"%c\") = %u\n", pt1, c, pt3 - pt1);
else
printf("strrchr(\"%s\", \"%c\") = NULL\n", pt1, c);

puts("");
pt1 = "1234567890";

/* strpbrk */
pt2 = "ABCD8";
pt3 = strpbrk(pt1, pt2);
if (pt3 != '\0')
printf("strpbrk(\"%s\", \"%s\") = %u\n", pt1, pt2, pt3 - pt1);
else
printf("strpbrk(\"%s\", \"%s\") = NULL\n", pt1, pt2);

pt2 = "ABCDE";
pt3 = strpbrk(pt1, pt2);
if (pt3 != '\0')
printf("strpbrk(\"%s\", \"%s\") = %u\n", pt1, pt2, pt3 - pt1);
else
printf("strpbrk(\"%s\", \"%s\") = NULL\n", pt1, pt2);

puts("");

/* strcspn */
pt2 = "0";
i = strcspn(pt1, pt2);
printf("strcspn(\"%s\", \"%s\") = %u\n", pt1, pt2, i);

pt2 = "A";
i = strcspn(pt1, pt2);
printf("strcspn(\"%s\", \"%s\") = %u\n", pt1, pt2, i);

puts("");

/* strtok */
pt1 = "a\\bcd\\efg";
pt2 = "\\";
printf("pt1 = %s\n", pt1);

pt3 = strtok(pt1, pt2);
printf("pt1 = %s, pt3 = %s\n", pt1, pt3);

pt3 = strtok('\0', pt2);
printf("pt1 = %s, pt3 = %s\n", pt1, pt3);

pt3 = strtok('\0', pt2);
printf("pt1 = %s, pt3 = %s\n", pt1, pt3);

return 0;
}

実行結果


R:\>lesson087\project1.exe
1234567890 + ABCDE = 1234567890ABCDE
1234567890 + ABCDE = 1234567890ABCD
1234567890 + ABCDE = 1234567890ABCDE

1234567890ABCDE
EDCBA0987654321

123 = 123 (0)
123 > 122 (1)
123 < 124 (-1)
123 < 999 (-8)
123 > 12 (51)
123 < 1234 (-52)

ABCD < abcd (-32)
ABCD = abcd (0)

strstr("1234567890", "45") = 3
strstr("1234567890", "12") = 0
strstr("1234567890", "123456780") = NULL

strchr("1294567890", "9") = 2
strchr("1294567890", "A") = NULL

strrchr("1294567890", "9") = 8
strrchr("1294567890", "A") = NULL

strpbrk("1234567890", "ABCD8") = 7
strpbrk("1234567890", "ABCDE") = NULL

strcspn("1234567890", "0") = 9
strcspn("1234567890", "A") = 10

pt1 = a\bcd\efg
pt1 = a, pt3 = a
pt1 = a, pt3 = bcd
pt1 = a, pt3 = efg

Delphi


program Project1;

{$APPTYPE CONSOLE}

uses
SysUtils, StrUtils;

procedure main();
const
st1: String = '1234567890';
st2: String = 'ABCDE';

pt1: array[0..255] of Char = '1234567890';
pt2: array[0..255] of Char = 'ABCDE';
var
st3: String;
pt3: array[0..255] of Char;
ptr: PChar;
c : Char;
i, j: Integer;
begin
(* Concat *)
st3 := st1 + st2;
Writeln(Format('%s + %s = %s', [st1, st2, st3]));

st3 := Concat(st1, st2);
Writeln(Format('%s + %s = %s', [st1, st2, st3]));

Writeln('');

(* Pos *)
st3 := '45';
i := Pos(st3, st1);
Writeln(Format('Pos(%s, %s) = %d', [st3, st1, i]));

st3 := '12';
i := Pos(st3, st1);
Writeln(Format('Pos(%s, %s) = %d', [st3, st1, i]));

st3 := '123456780';
i := Pos(st3, st1);
Writeln(Format('Pos(%s, %s) = %d', [st3, st1, i]));

Writeln('');

(* LeftStr *)
i := 5;
st3 := LeftStr(st1, i);
Writeln(Format('LeftStr(%s, %d) = %s', [st1, i, st3]));

(* MidStr *)
i := 5;
j := 3;
st3 := MidStr(st1, i, j);
Writeln(Format('MidStr(%s, %d, %d) = %s', [st1, i, j, st3]));

(* RightStr *)
i := 5;
st3 := RightStr(st1, i);
Writeln(Format('RightStr(%s, %d) = %s', [st1, i, st3]));

Writeln('');

(* ReverseString *)
st3 := ReverseString(st1);
Writeln(Format('ReverseString(%s) = %s', [st1, st3]));

Writeln('');

(* StuffString *)
i := 5;
j := 3;
st3 := StuffString(st1, i, j, st2);
Writeln(Format('StuffString(%s, %d, %d, %s) = %s', [st1, i, j, st2, st3]));

Writeln('');

(* StringReplace *)
st3 := StringReplace('AbcAbcAbc', 'ABC', '123', [rfIgnoreCase]);
Writeln(Format('StringReplace(''AbcAbcAbc'', ''ABC'', ''123'', [rfIgnoreCase]) = %s', [st3]));

st3 := StringReplace('AbcAbcAbc', 'ABC', '123', [rfIgnoreCase, rfReplaceAll]);
Writeln(Format('StringReplace(''AbcAbcAbc'', ''ABC'', ''123'', [rfIgnoreCase, rfReplaceAll]) = %s', [st3]));

st3 := StringReplace('AbcABCABC', 'ABC', '123', []);
Writeln(Format('StringReplace(''AbcABCABC'', ''ABC'', ''123'', []) = %s', [st3]));

Writeln('');

(* Insert *)
i := 5;
st3 := '1234567890';
st2 := 'ABC';
write(Format('Insert(%s, %s, %d) = ', [st2, st3, i]));
Insert(st2, st3, i);
Writeln(Format('%s', [st3]));

(* Delete *)
i := 5;
j := 3;
st3 := '1234567890';
write(Format('Delete(%s, %d, %d) = ', [st3, i, j]));
Delete(st3, i, j);
Writeln(Format('%s', [st3]));

Writeln('');

(* StrCat *)
pt3 := '1234567890';
write(Format('%s + %s = ', [pt3, pt2]));
StrCat(pt3, pt2);
Writeln(Format('%s', [pt3]));

Writeln('');

(* StrComp *)
pt1 := '123';
pt2 := '123';
i := StrComp(pt1, pt2);
if (i = 0) then
Writeln(Format('%s = %s (%d)', [pt1, pt2, i]))
else if (i > 0) then
Writeln(Format('%s = %s (%d)', [pt1, pt2, i]))
else
Writeln(Format('%s = %s (%d)', [pt1, pt2, i]));

pt2 := '122';
i := StrComp(pt1, pt2);
if (i = 0) then
Writeln(Format('%s = %s (%d)', [pt1, pt2, i]))
else if (i > 0) then
Writeln(Format('%s = %s (%d)', [pt1, pt2, i]))
else
Writeln(Format('%s = %s (%d)', [pt1, pt2, i]));

pt2 := '124';
i := StrComp(pt1, pt2);
if (i = 0) then
Writeln(Format('%s = %s (%d)', [pt1, pt2, i]))
else if (i > 0) then
Writeln(Format('%s = %s (%d)', [pt1, pt2, i]))
else
Writeln(Format('%s = %s (%d)', [pt1, pt2, i]));

pt2 := '999';
i := StrComp(pt1, pt2);
if (i = 0) then
Writeln(Format('%s = %s (%d)', [pt1, pt2, i]))
else if (i > 0) then
Writeln(Format('%s = %s (%d)', [pt1, pt2, i]))
else
Writeln(Format('%s = %s (%d)', [pt1, pt2, i]));

pt2 := '12';
i := StrComp(pt1, pt2);
if (i = 0) then
Writeln(Format('%s = %s (%d)', [pt1, pt2, i]))
else if (i > 0) then
Writeln(Format('%s = %s (%d)', [pt1, pt2, i]))
else
Writeln(Format('%s = %s (%d)', [pt1, pt2, i]));

pt2 := '1234';
i := StrComp(pt1, pt2);
if (i = 0) then
Writeln(Format('%s = %s (%d)', [pt1, pt2, i]))
else if (i > 0) then
Writeln(Format('%s = %s (%d)', [pt1, pt2, i]))
else
Writeln(Format('%s = %s (%d)', [pt1, pt2, i]));

Writeln('');

(* StrIComp *)
pt1 := 'ABCD';
pt2 := 'abcd';
i := StrComp(pt1, pt2);
if (i = 0) then
Writeln(Format('%s = %s (%d)', [pt1, pt2, i]))
else if (i > 0) then
Writeln(Format('%s = %s (%d)', [pt1, pt2, i]))
else
Writeln(Format('%s = %s (%d)', [pt1, pt2, i]));

i := StrIComp(pt1, pt2);
if (i = 0) then
Writeln(Format('%s = %s (%d)', [pt1, pt2, i]))
else if (i > 0) then
Writeln(Format('%s = %s (%d)', [pt1, pt2, i]))
else
Writeln(Format('%s = %s (%d)', [pt1, pt2, i]));

Writeln('');

(* StrPos *)
pt1 := '1234567890';
pt2 := '45';
ptr := StrPos(pt1, pt2);
Writeln(Format('StrPos(%s, %s) = %d', [pt1, pt2, ptr - pt1]));

pt2 := '12';
ptr := StrPos(pt1, pt2);
Writeln(Format('StrPos(%s, %s) = %d', [pt1, pt2, ptr - pt1]));

pt2 := '123456780';
ptr := StrPos(pt1, pt2);
if (ptr = nil) then
Writeln(Format('StrPos(%s, %s) = nil', [pt1, pt2]))
else
Writeln(Format('StrPos(%s, %s) = %d', [pt1, pt2, ptr - pt1]));

Writeln('');

(* StrScan *)
pt1 := '1294567890';
c := '9';
ptr := StrScan(pt1, c);
Writeln(Format('StrScan(%s, %s) = %d', [pt1, c, ptr - pt1]));

c := 'A';
ptr := StrScan(pt1, c);
if (ptr = nil) then
Writeln(Format('StrScan(%s, %s) = nil', [pt1, c]))
else
Writeln(Format('StrScan(%s, %s) = %d', [pt1, c, ptr - pt1]));

Writeln('');

(* StrRScan *)
c := '9';
ptr := StrRScan(pt1, c);
Writeln(Format('StrRScan(%s, %s) = %d', [pt1, c, ptr - pt1]));

c := 'A';
ptr := StrRScan(pt1, c);
if (ptr = nil) then
Writeln(Format('StrRScan(%s, %s) = nil', [pt1, c]))
else
Writeln(Format('StrRScan(%s, %s) = %d', [pt1, c, ptr - pt1]));
end;

begin
main;
end.

実行結果


S:\>lesson087\project1.exe
1234567890 + ABCDE = 1234567890ABCDE
1234567890 + ABCDE = 1234567890ABCDE

Pos(45, 1234567890) = 4
Pos(12, 1234567890) = 1
Pos(123456780, 1234567890) = 0

LeftStr(1234567890, 5) = 12345
MidStr(1234567890, 5, 3) = 567
RightStr(1234567890, 5) = 67890

ReverseString(1234567890) = 0987654321

StuffString(1234567890, 5, 3, ABCDE) = 1234ABCDE890

StringReplace('AbcAbcAbc', 'ABC', '123', [rfIgnoreCase]) = 123AbcAbc
StringReplace('AbcAbcAbc', 'ABC', '123', [rfIgnoreCase, rfReplaceAll]) = 123123123
StringReplace('AbcABCABC', 'ABC', '123', []) = Abc123ABC

Insert(ABC, 1234567890, 5) = 1234ABC567890
Delete(1234567890, 5, 3) = 1234890

1234567890 + ABCDE = 1234567890ABCDE

123 = 123 (0)
123 = 122 (1)
123 = 124 (-1)
123 = 999 (-8)
123 = 12 (51)
123 = 1234 (-52)

ABCD = abcd (-32)
ABCD = abcd (0)

StrPos(1234567890, 45) = 3
StrPos(1234567890, 12) = 0
StrPos(1234567890, 123456780) = nil

StrScan(1294567890, 9) = 2
StrScan(1294567890, A) = nil

StrRScan(1294567890, 9) = 8
StrRScan(1294567890, A) = nil

Perl
# strcat 
$st1 = "1234567890";
$st2 = "ABCDE";

$st3 = $st1 . $st2;
printf("%s + ", $st1);
printf("%s = ", $st2);
printf("%s\n", $st3);

$st3 = join('', ($st1, $st2));
printf("%s + ", $st1);
printf("%s = ", $st2);
printf("%s\n", $st3);
printf("\n");

# strrev 
printf("%s\n", $st3);
@ary = split(//, $st3);
@ary = reverse(@ary);
$st3 = join('', @ary);
printf("%s\n", $st3);
printf("\n");



# strcmp 
$pt1 = "123";
$pt2 = "123";

$i = ($pt1 cmp $pt2);
if ($i == 0)
{
    printf("%s = %s (%d)\n", $pt1, $pt2, $i);
}
elsif ($i > 0)
{
    printf("%s > %s (%d)\n", $pt1, $pt2, $i);
}
else
{
    printf("%s < %s (%d)\n", $pt1, $pt2, $i);
}

$pt2 = "122";
$i = ($pt1 cmp $pt2);
if ($i == 0)
{
    printf("%s = %s (%d)\n", $pt1, $pt2, $i);
}
elsif ($i > 0)
{
    printf("%s > %s (%d)\n", $pt1, $pt2, $i);
}
else
{
    printf("%s < %s (%d)\n", $pt1, $pt2, $i);
}

$pt2 = "124";
$i = ($pt1 cmp $pt2);
if ($i == 0)
{
    printf("%s = %s (%d)\n", $pt1, $pt2, $i);
}
elsif ($i > 0)
{
    printf("%s > %s (%d)\n", $pt1, $pt2, $i);
}
else
{
    printf("%s < %s (%d)\n", $pt1, $pt2, $i);
}

$pt2 = "999";
$i = ($pt1 cmp $pt2);
if ($i == 0)
{
    printf("%s = %s (%d)\n", $pt1, $pt2, $i);
}
elsif ($i > 0)
{
    printf("%s > %s (%d)\n", $pt1, $pt2, $i);
}
else
{
    printf("%s < %s (%d)\n", $pt1, $pt2, $i);
}

$pt2 = "12";
$i = ($pt1 cmp $pt2);
if ($i == 0)
{
    printf("%s = %s (%d)\n", $pt1, $pt2, $i);
}
elsif ($i > 0)
{
    printf("%s > %s (%d)\n", $pt1, $pt2, $i);
}
else
{
    printf("%s < %s (%d)\n", $pt1, $pt2, $i);
}

$pt2 = "1234";
$i = ($pt1 cmp $pt2);
if ($i == 0)
{
    printf("%s = %s (%d)\n", $pt1, $pt2, $i);
}
elsif ($i > 0)
{
    printf("%s > %s (%d)\n", $pt1, $pt2, $i);
}
else
{
    printf("%s < %s (%d)\n", $pt1, $pt2, $i);
}

print "\n";


# strstr 
$pt1 = "1234567890";
$pt2 = "45";
$i = index($pt1, $pt2);
printf("index(\"%s\", \"%s\") = %d\n", $pt1, $pt2, $i);

$pt2 = "12";
$i = index($pt1, $pt2);
printf("index(\"%s\", \"%s\") = %d\n", $pt1, $pt2, $i);

$pt2 = "123456780";
$i = index($pt1, $pt2);
printf("index(\"%s\", \"%s\") = %d\n", $pt1, $pt2, $i);

print "\n";


# strrchr 
$c = "9";
$i = rindex($pt1, $c);
printf("rindex(\"%s\", \"%s\") = %s\n", $pt1, $c, $i);

$c = "A";
$i = rindex($pt1, $c);
printf("rindex(\"%s\", \"%s\") = %s\n", $pt1, $c, $i);

print "\n";



# s
$pt1 = "aabbccabcAABBCCABC";
print $pt1, "\n";

$pt1 =~ s/a/1/; # "a" を "1" に
print "s/a/1/ = ", $pt1, "\n";

$pt1 = "aabbccabcAABBCCABC";
$pt1 =~ s/a/1/g; # "a" を "1" に
print "s/a/1/g = ", $pt1, "\n";

$pt1 = "aabbccabcAABBCCABC";
$pt1 =~ s/a/1/gi; # "a" を "1" に
print "s/a/1/gi = ", $pt1, "\n";

$pt1 = "aabbccabcAABBCCABC";
$pt1 =~ s/abc/123/gi; # "abc" を "123" に
print "s/abc/123/gi = ", $pt1, "\n";

$pt1 = "aabbccabcAABBCCABC";
$pt1 =~ s/abc/12/gi; # "abc" を "12" に
print "s/abc/12/gi = ", $pt1, "\n";

$pt1 = "aabbccabcAABBCCABC";
$pt1 =~ s/ac/13/gi; # "ac" を "13" に
print "s/ac/13/gi = ", $pt1, "\n";
print "\n";


$pt1 = "aabbccabcAABBCCABC";
$pt1 =~ tr/a/1/; # "a" を "1" に
print "tr/a/1/ = ", $pt1, "\n";

$pt1 = "aabbccabcAABBCCABC";
$pt1 =~ tr/a/1/c; # "a"以外 を "1" に
print "tr/a/1/c = ", $pt1, "\n";

$pt1 = "aabbccabcAABBCCABC";
$pt1 =~ tr/a/1/d; # "a" を "1" に
print "tr/a/1/d = ", $pt1, "\n";

$pt1 = "aabbccabcAABBCCABC";
$pt1 =~ tr/a/1/s; # "a"の連続を "1" に
print "tr/a/1/s = ", $pt1, "\n";

$pt1 = "aabbccabcAABBCCABC";
$pt1 =~ tr/abc/123/; # "a", "b", "c" を "1", "2", "3" に
print "tr/abc/123/ = ", $pt1, "\n";

$pt1 = "aabbccabcAABBCCABC";
$pt1 =~ tr/abc/1/c; # "a", "b", "c" 以外 を "1" に
print "tr/abc/1/c = ", $pt1, "\n";

$pt1 = "aabbccabcAABBCCABC";
$pt1 =~ tr/abc/123/d; # "a", "b", "c" を "1", "2", "3" に
print "tr/abc/123/d = ", $pt1, "\n";

$pt1 = "aabbccabcAABBCCABC";
$pt1 =~ tr/abc/12/d; # "a", "b" を "1", "2" に。"c" は 削除
print "tr/abc/12/d = ", $pt1, "\n";

$pt1 = "aabbccabcAABBCCABC";
$pt1 =~ tr/abc/123/s; # "a"の連続を "1" に。 "b"の連続を "2" に。 "c"の連続を "3" に。
print "tr/abc/123/s = ", $pt1, "\n";

実行結果

L:\>perl lesson_11_087.pl
1234567890 + ABCDE = 1234567890ABCDE
1234567890 + ABCDE = 1234567890ABCDE

1234567890ABCDE
EDCBA0987654321

123 = 123 (0)
123 > 122 (1)
123 < 124 (-1)
123 < 999 (-1)
123 > 12 (1)
123 < 1234 (-1)

index("1234567890", "45") = 3
index("1234567890", "12") = 0
index("1234567890", "123456780") = -1

rindex("1234567890", "9") = 8
rindex("1234567890", "A") = -1

aabbccabcAABBCCABC
s/a/1/ = 1abbccabcAABBCCABC
s/a/1/g = 11bbcc1bcAABBCCABC
s/a/1/gi = 11bbcc1bc11BBCC1BC
s/abc/123/gi = aabbcc123AABBCC123
s/abc/12/gi = aabbcc12AABBCC12
s/ac/13/gi = aabbccabcAABBCCABC

tr/a/1/ = 11bbcc1bcAABBCCABC
tr/a/1/c = aa1111a11111111111
tr/a/1/d = 11bbcc1bcAABBCCABC
tr/a/1/s = 1bbcc1bcAABBCCABC
tr/abc/123/ = 112233123AABBCCABC
tr/abc/1/c = aabbccabc111111111
tr/abc/123/d = 112233123AABBCCABC
tr/abc/12/d = 112212AABBCCABC
tr/abc/123/s = 123123AABBCCABC

Ruby
# strcat 
st1 = "1234567890"
st2 = "ABCDE"

st3 = st1 + st2
printf("%s + ", st1)
printf("%s = ", st2)
printf("%s\n", st3)

st3 = st1 << st2
printf("%s + ", st1)
printf("%s = ", st2)
printf("%s\n", st3)

st3 = st1.concat(st2)
printf("%s + ", st1)
printf("%s = ", st2)
printf("%s\n", st3)
puts ""


# strrev 
printf("%s\n", st3)
ary = st3.split(//)
ary = ary.reverse
st3 = ary.join
printf("%s\n", st3)

printf("%s\n", st3.reverse)
puts ""


# strcmp 
pt1 = "123"
pt2 = "123"

i = pt1 <=> pt2
if (i == 0)
    printf("%s = %s (%d)\n", pt1, pt2, i)
elsif (i > 0)
    printf("%s > %s (%d)\n", pt1, pt2, i)
else
    printf("%s < %s (%d)\n", pt1, pt2, i)
end


pt2 = "122"
i = pt1 <=> pt2
if (i == 0)
    printf("%s = %s (%d)\n", pt1, pt2, i)
elsif (i > 0)
    printf("%s > %s (%d)\n", pt1, pt2, i)
else
    printf("%s < %s (%d)\n", pt1, pt2, i)
end

pt2 = "124"
i = pt1 <=> pt2
if (i == 0)
    printf("%s = %s (%d)\n", pt1, pt2, i)
elsif (i > 0)
    printf("%s > %s (%d)\n", pt1, pt2, i)
else
    printf("%s < %s (%d)\n", pt1, pt2, i)
end

pt2 = "999"
i = pt1 <=> pt2
if (i == 0)
    printf("%s = %s (%d)\n", pt1, pt2, i)
elsif (i > 0)
    printf("%s > %s (%d)\n", pt1, pt2, i)
else
    printf("%s < %s (%d)\n", pt1, pt2, i)
end

pt2 = "12"
i = pt1 <=> pt2
if (i == 0)
    printf("%s = %s (%d)\n", pt1, pt2, i)
elsif (i > 0)
    printf("%s > %s (%d)\n", pt1, pt2, i)
else
    printf("%s < %s (%d)\n", pt1, pt2, i)
end

pt2 = "1234"
i = pt1 <=> pt2
if (i == 0)
    printf("%s = %s (%d)\n", pt1, pt2, i)
elsif (i > 0)
    printf("%s > %s (%d)\n", pt1, pt2, i)
else
    printf("%s < %s (%d)\n", pt1, pt2, i)
end

puts ""


# strstr 
pt1 = "1234567890"
pt2 = "45"
i = pt1.index(pt2)
if i == nil
    printf("index(\"%s\", \"%s\") = nil\n", pt1, pt2)
else
    printf("index(\"%s\", \"%s\") = %d\n", pt1, pt2, i)
end

pt2 = "12"
i = pt1.index(pt2)
if i == nil
    printf("index(\"%s\", \"%s\") = nil\n", pt1, pt2)
else
    printf("index(\"%s\", \"%s\") = %d\n", pt1, pt2, i)
end

pt2 = "123456780"
i = pt1.index(pt2)
if i == nil
    printf("index(\"%s\", \"%s\") = nil\n", pt1, pt2)
else
    printf("index(\"%s\", \"%s\") = %d\n", pt1, pt2, i)
end

puts ""


# strrchr 
c = "9"
i = pt1.rindex(c)
if i == nil
    printf("rindex(\"%s\", \"%s\") = nil\n", pt1, c)
else
    printf("rindex(\"%s\", \"%s\") = %s\n", pt1, c, i)
end

c = "A"
i = pt1.rindex(c)
if i == nil
    printf("rindex(\"%s\", \"%s\") = nil\n", pt1, c)
else
    printf("rindex(\"%s\", \"%s\") = %s\n", pt1, c, i)
end

puts ""



# s
pt1 = "aabbccabcAABBCCABC"
puts pt1

pt1.sub!(/a/, "1") # "a" を "1" に
print "s/a/1/ = ", pt1, "\n"

pt1 = "aabbccabcAABBCCABC"
pt1.gsub!(/a/, "1") # "a" を "1" に
print "s/a/1/g = ", pt1, "\n"

pt1 = "aabbccabcAABBCCABC"
pt1.gsub!(/a/i, "1") # "a" を "1" に
print "s/a/1/gi = ", pt1, "\n"

pt1 = "aabbccabcAABBCCABC"
pt1.gsub!(/abc/i, "123") # "abc" を "123" に
print "s/abc/123/gi = ", pt1, "\n"

pt1 = "aabbccabcAABBCCABC"
pt1.gsub!(/abc/i, "12") # "abc" を "12" に
print "s/abc/12/gi = ", pt1, "\n"

pt1 = "aabbccabcAABBCCABC"
pt1.gsub!(/ac/i, "13") # "ac" を "13" に
print "s/ac/13/gi = ", pt1, "\n"

pt1 = "aabbccabcAABBCCABC"
pt1.tr!("a", "1") # "a" を "1" に
print "tr/a/1/ = ", pt1, "\n"

pt1 = "aabbccabcAABBCCABC"
pt1.tr!("^a", "1") # "a"以外 を "1" に
print "tr/a/1/c = ", pt1, "\n"

pt1 = "aabbccabcAABBCCABC"
pt1.tr_s!("a", "1") # "a"の連続を "1" に
print "tr/a/1/s = ", pt1, "\n"

pt1 = "aabbccabcAABBCCABC"
pt1.tr!("abc", "123") # "a", "b", "c" を "1", "2", "3" に
print "tr/abc/123/ = ", pt1, "\n"

pt1 = "aabbccabcAABBCCABC"
pt1.tr!("^abc", "1") # "a", "b", "c" 以外 を "1" に
print "tr/abc/1/ = ", pt1, "\n"

pt1 = "aabbccabcAABBCCABC"
pt1.tr!("abc", "12") # "a", "b", "c" を "1", "2", "2" に
print "tr/abc/12/d = ", pt1, "\n"

pt1 = "aabbccabcAABBCCABC"
pt1.tr_s!("abc", "123") # "a"の連続を "1" に。 "b"の連続を "2" に。 "c"の連続を "3" に。
print "tr/abc/123/s = ", pt1, "\n"

実行結果

L:\>ruby l:\lesson_11_087.rb
1234567890 + ABCDE = 1234567890ABCDE
1234567890ABCDE + ABCDE = 1234567890ABCDE
1234567890ABCDEABCDE + ABCDE = 1234567890ABCDEABCDE

1234567890ABCDEABCDE
EDCBAEDCBA0987654321
1234567890ABCDEABCDE

123 = 123 (0)
123 > 122 (1)
123 < 124 (-1)
123 < 999 (-1)
123 > 12 (1)
123 < 1234 (-1)

index("1234567890", "45") = 3
index("1234567890", "12") = 0
index("1234567890", "123456780") = nil

rindex("1234567890", "9") = 8
rindex("1234567890", "A") = nil

aabbccabcAABBCCABC
s/a/1/ = 1abbccabcAABBCCABC
s/a/1/g = 11bbcc1bcAABBCCABC
s/a/1/gi = 11bbcc1bc11BBCC1BC
s/abc/123/gi = aabbcc123AABBCC123
s/abc/12/gi = aabbcc12AABBCC12
s/ac/13/gi = aabbccabcAABBCCABC
tr/a/1/ = 11bbcc1bcAABBCCABC
tr/a/1/c = aa1111a11111111111
tr/a/1/s = 1bbcc1bcAABBCCABC
tr/abc/123/ = 112233123AABBCCABC
tr/abc/1/ = aabbccabc111111111
tr/abc/12/d = 112222122AABBCCABC
tr/abc/123/s = 123123AABBCCABC