才皚

玡 ヘ魁 

 ﹍ての结 盽ǎ才﹃ㄧ计

C++ Τㄢ贺よ猭笷才﹃才皚才ボ夹硂ㄠи癚阶才皚

硂い┮Τ才皚砆跌才﹃


 Top

琌才皚ㄒ

C++ セ 单 Turbo Pascal セ
char s[10]; var s: string[10];

 C++ い才﹃挡Ю矪Τ才ㄤ ASCII 絏 0 C++ い '\0' ボ璶ノ才皚纗程Τ n 才才﹃赣皚程琌 n + 1 程琌ノㄓ纗才


﹍ての结 Top

и才皚рウ﹍てㄒ

char s[] = "Hello"; 单 char s[6] = {'H', 'e', 'l', 'l', 'o', '\0'};

и祘い丁р才皚﹍てぃ筁иゲ斗ㄏノ strcpy 才﹃ㄧ计璶ノ硂ㄧ计祘程秨繷よゲ斗Τ玡竚矪瞶竟#include <string.h>


盽ǎ才﹃ㄧ计 Top

璶ㄏノㄧ计祘程秨繷よゲ斗Τ玡竚矪瞶竟#include <string.h>

盽ǎ才﹃ㄧ计 種竡
strcpy(s, t) р才﹃ t й才﹃ s
strncpy(s, t, n) р才﹃ t い程 n 才й才﹃ s
狦 t  >= n'\0' ぃ穦砆笆程
strcat(s, t) р才﹃ t 钡婚才﹃ s 挡Ю矪
strncat(s, t, n) р才﹃ t い程 n 才钡婚才﹃   ss 挡Ю矪 '\0'
strlen(s) 肚才﹃ s 
strcmp(s, t) ゑ耕才﹃ s ㎝才﹃ ts < t肚璽 s = t肚箂 s > t肚タ
strncmp(s, t, n) ゑ耕才﹃ s ㎝才﹃ t 程 n 才 s < t肚璽 s = t肚箂 s > t肚タ

祘絛ㄒ

祘きㄏノ才﹃ㄧ计
#include <string.h>
#include <iostream.h>

main()
{
   char a[] = "C++ programming", b[50], c[50], d[50];
   int i;

   cout << "a contains " <<
strlen(a) << " characters\n"; // 15 才

  
strcpy(b, "Welcome to this program!");                 // b 砆﹍て
  
strncpy(c, b, 11);                                     // "Welcome to " 纗 c ず
  
   // 粂琌ゲ斗 strncpy ぃ穦才﹃挡Ю矪笆 '\0'
  
c[11] = '\0';

  
strcpy(d, c);                           // "Welcome to " 纗 d ず
  
strncat(c, a, 3);                       // "Welcome to C++" 纗 c ず
  
strcat(d, a);                           // "Welcome to C++ programming" 纗 d ず

   // b: "Welcome to this program!"
   // c: "Welcome to C++"
   // d: "Welcome to C++ programming"
   cout << "b: " << b << "\nc: " << c << "\nd: " << d << endl;

   // ǎ b > d
   if (strcmp(b, d) > 0)
     cout << "b > d\n";
   else if (strcmp(b, d) == 0)
     cout << "b = d\n";
   else
     cout << "b < d\n";


  
for (i = 0; !strncmp(b, d, i); i++) ;                   // ⊿Τ碻吏肈
   // 讽 strncmp(b, d, i) == 0 碻吏碞穦瞒秨
   cout << "b and d differ at character " << i << "." << endl;
  
   return 0;
}
块絛ㄒ
a contains 15 characters
b: Welcome to this program!
c: Welcome to C++
d: Welcome to C++ programming
b > d
b and d differ at character 12.

玡 ヘ魁 