2012年1月28日土曜日

[C/C++] 新・明解C++入門編 8-2「cstringライブラリ」

書籍「新・明解C++入門編 (明解シリーズ)」の中に収められているプログラム等を実際にコンパイル,実行してみた際のメモです.

List 8-12
#include <cstddef>

size_t strlen(const char* s)
{
  size_t len = 0;

  while (*s++)
    len++;
  return len; 

}

List 8-13
#include <cstddef>

size_t strlen(const char* s)
{
    const char* p = s;

  while (*s)
    s++;
  return s - p;

}

List 8-14
#include <cstring>
#include <iostream>

using namespace std;

int main()
{
  char str[100];
  cout << "Input a character string: ";
  cin >> str;

  cout << "The length of the string " << str << "\" is " << strlen(str) << ". \n"

}
コンパイル,実行結果:
$ g++ List8-14.cpp
$ ./a.out
Input a character string: five
The length of the string five" is 4. 

List 8-15
#include <cstring>
#include <iostream>

using namespace std;

int main()
{
  char s1[8] = "";
  char s2[8] = "ABC";
  char s3[8] = "AB\0CDEF";

  cout << "strlen(s1)         = "     << strlen(s1)     << '\n';
  cout << "strlen(s2)         = "     << strlen(s2)     << '\n';
  cout << "strlen(&s2[1])     = "     << strlen(&s2[1]) << '\n';
  cout << "strlen(s3)         = "     << strlen(s3)     << '\n';
  cout << "strlen(\"XYZ\")      = "   << strlen("XYZ")  << '\n';
  cout << "strlen(&\"XYZ\"[1])      = "   << strlen(&"XYZ"[1])  << '\n';
  cout << "strlen(\"ABC\\0DEF\")  = "   << strlen("ABC\0DEF") << '\n';
  cout << "sizeof(\"ABC\\0DEF\")   = "   << sizeof("ABC\0DEF") << '\n';

}
コンパイル,実行結果:
$ g++ List8-15.cpp
$ ./a.out
strlen(s1)         = 0
strlen(s2)         = 3
strlen(&s2[1])     = 2
strlen(s3)         = 2
strlen("XYZ")      = 3
strlen(&"XYZ"[1])      = 2
strlen("ABC\0DEF")  = 3
sizeof("ABC\0DEF")   = 8

List 8-16
#include <cstring>
#include <iostream>

using namespace std;

int main()
{
  char tmp[16];
  char s1[16], s2[16], s3[16];

  cout << "Input Characters: ";
  cin >> tmp;

  strcpy(s1, strcpy(s2, tmp));

  cout << "Characters s1 is \"" << s1 << "\". \n";
  cout << "Characters s2 is \"" << s2 << "\". \n";
  cout << "Characters s3 is \"" << strcpy(s3, tmp) << "\". \n";

  char* x = "XXXXXXXXX";

  strcpy(s3, x); strncpy(s3, "12345", 3);
    cout << "s3 = " << s3 << '\n';
    strcpy(s3, x); strncpy(s3, "12345", 5);
      cout << "s3 = " << s3 << '\n';
    strcpy(s3, x); strncpy(s3, "12345", 7);
      cout << "s3 = " << s3 << '\n';
    strcpy(s3, x); strncpy(s3, "1234567890", 9);
      cout << "s3 = " << s3 << '\n';

}
コンパイル,実行結果:
$ g++ List8-16.cpp
List8-16.cpp:20:13: warning: conversion from string literal to 'char *' is deprecated
      [-Wc++11-compat-deprecated-writable-strings]
  char* x = "XXXXXXXXX";
            ^
1 warning generated.
MBP:desktop hide$ ./a.out
Input Characters: ABC
Characters s1 is "ABC". 
Characters s2 is "ABC". 
Characters s3 is "ABC". 
s3 = 123XXXXXX
s3 = 12345XXXX
s3 = 12345
s3 = 123456789
警告が出ますが,実行可能です.

List 8-17
#include <cstring>
#include <iostream>

using namespace std;

int main()
{
  char s[10];
  char* x = "ABC";

  strcpy(s, "QWE");
  strcat(s, "RTY");
  cout << "s = " << s << '\n';

  strcpy(s, x); strncat(s, "123", 1);
    cout << "s = " << s << '\n';
  strcpy(s, x); strncat(s, "123", 3);
    cout << "s = " << s << '\n';
  strcpy(s, x); strncat(s, "123", 5);
    cout << "s = " << s << '\n';
  strcpy(s, x); strncat(s, "12345", 5);
    cout << "s = " << s << '\n';
  strcpy(s, x); strncat(s, "123456789", 5);
    cout << "s = " << s << '\n'
}
コンパイル,実行結果:

$ g++ List8-17.cpp
List8-17.cpp:9:13: warning: conversion from string literal to 'char *' is deprecated
      [-Wc++11-compat-deprecated-writable-strings]
  char* x = "ABC";
            ^
1 warning generated.
$ ./a.out
s = QWERTY
s = ABC1
s = ABC123
s = ABC123
s = ABC12345
s = ABC12345

List 8-18

#include <cstring>
#include <iostream>

using namespace std;

int main()
{
  char st[128];

  cout << "Compare to\"ABCDE\". \n";
  cout << "Stop \"XXXXX\"\n"

  while (1) {
    cout << "\n Characterst: ";
      cin >> st;

      if (strcmp(st, "XXXXX") == 0)
        break;
      cout << "strcmp( \"ABCDE\", st)    = " << strcmp( "ABCDE", st) << '\n';
      cout << "strncmp(\"ABCDE\", st, 3) = " << strncmp("ABCDE", st, 3) << '\n';
  }
}
コンパイル,実行結果:

$ g++ List8-18.cpp
$ ./a.out
Compare to"ABCDE". 
Stop "XXXXX"

 Characterst: ABC
strcmp( "ABCDE", st)    = 68
strncmp("ABCDE", st, 3) = 0

 Characterst: ABCDE
strcmp( "ABCDE", st)    = 0
strncmp("ABCDE", st, 3) = 0

 Characterst: AX
strcmp( "ABCDE", st)    = -22
strncmp("ABCDE", st, 3) = -22

 Characterst: XXXXX

0 件のコメント :

コメントを投稿