C++ Τㄢ贺よ猭笷才﹃才皚の才ボ夹硂ㄠи癚阶才皚
硂い┮Τ才皚砆跌才﹃
琌才皚ㄒ
C++ セ | 单 Turbo Pascal セ |
char s[10]; | var s: string[10]; |
C++ い才﹃挡Ю矪Τ才ㄤ ASCII 絏 0 C++ い '\0' ボ璶ノ才皚纗程Τ n 才才﹃赣皚程琌 n + 1 程琌ノㄓ纗才
и才皚рウ﹍てㄒ
char s[] = "Hello"; | 单 | char s[6] = {'H', 'e', 'l', 'l', 'o', '\0'}; |
и祘い丁р才皚﹍てぃ筁иゲ斗ㄏノ strcpy 才﹃ㄧ计璶ノ硂ㄧ计祘程秨繷よゲ斗Τ玡竚矪瞶竟#include <string.h>
璶ㄏノㄧ计祘程秨繷よゲ斗Τ玡竚矪瞶竟#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. |