馃巿涓汉涓婚〉锛歔鐢滅編鐨勬睙]
馃鏀跺綍涓撴爮锛歔c璇█]
寮曡█锛?/strong>
鍦–璇█涓紝澶勭悊瀛楃涓叉槸涓€椤瑰父瑙佷笖閲嶈鐨勪换鍔°€備负浜嗘湁鏁堝湴鎿嶄綔瀛楃涓诧紝C璇█鎻愪緵浜嗚澶氬唴缃殑瀛楃涓插鐞嗗嚱鏁般€?/p>
鏈枃灏嗕粙缁嶄竴浜涘父瑙佺殑C璇█瀛楃涓插嚱鏁帮紝鍖呮嫭瀛楃涓叉悳绱㈠嚱鏁般€佸瓧绗︿覆闀垮害鍑芥暟鍜屽瓧绗︿覆鍒嗗壊鍑芥暟銆?/p>
閫氳繃浜嗚В杩欎簺鍑芥暟鐨勭敤娉曞拰鍘熺悊锛屼綘灏嗚兘澶熸洿杞绘澗鍦板鐞嗗拰鎿嶄綔瀛楃涓叉暟鎹€?/p>
涓€銆佸瓧绗︿覆鎼滅储鍑芥暟
褰撳湪C璇█涓渶瑕佸湪瀛楃涓蹭腑鎼滅储鐗瑰畾瀛楃鎴栧瓙瀛楃涓叉椂锛屽彲浠ヤ娇鐢ㄤ互涓嬪嚑涓父瑙佺殑瀛楃涓叉悳绱㈠嚱鏁帮細strchr銆乻trrchr 鍜?strstr銆備笅闈㈠皢璇︾粏浠嬬粛瀹冧滑鐨勭敤娉曘€佺ず渚嬩互鍙婃敞鎰忎簨椤广€?/p>
1.1 strchr (String Character):
鐢ㄦ硶锛?/strong>
strchr 鍑芥暟鐢ㄤ簬鍦ㄥ瓧绗︿覆涓煡鎵炬寚瀹氬瓧绗︾殑绗竴涓尮閰嶉」锛屽苟杩斿洖璇ュ瓧绗︽墍鍦ㄤ綅缃殑鎸囬拡銆傚鏋滄湭鎵惧埌鎸囧畾瀛楃锛屽垯杩斿洖 NULL銆?/p>
绀轰緥锛?/strong>
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "hello world";
char *ptr;
ptr = strchr(str, 'o');
if (ptr != NULL) {
printf("Found at position: %ld\n", ptr - str);
} else {
printf("Not found\n");
}
return 0;
}
缁撴灉锛?/strong>
Found at position: 4
浠g爜鍒嗘瀽锛?/strong>
杩欐C浠g爜棣栧厛瀹氫箟浜嗕竴涓瓧绗︽暟缁?str 鍖呭惈瀛楃涓?"hello world"銆傜劧鍚庯紝閫氳繃璋冪敤 strchr 鍑芥暟锛屽湪瀛楃涓蹭腑鏌ユ壘瀛楃 'o' 鐨勭涓€娆″嚭鐜扮殑浣嶇疆锛屽苟灏嗚浣嶇疆鐨勬寚閽堣祴鍊肩粰 ptr 鍙橀噺銆?/p>
鎺ョ潃锛岄€氳繃鏉′欢鍒ゆ柇妫€鏌?ptr 鏄惁涓?NULL锛屽鏋滀笉鏄紝鍒欒〃绀烘壘鍒颁簡瀛楃 'o'锛屽苟浣跨敤 printf 鍑芥暟杈撳嚭鍏跺湪瀛楃涓蹭腑鐨勪綅缃紙閫氳繃鎸囬拡鍑忔硶璁$畻鐩稿浣嶇疆锛夛紝鍚﹀垯杈撳嚭 "Not found"銆?/p>
鏈€鍚庯紝绋嬪簭杩斿洖0锛岃〃绀烘甯告墽琛岀粨鏉熴€?/p>
杩欐浠g爜鐨勪綔鐢ㄦ槸鍦ㄥ瓧绗︿覆涓煡鎵惧瓧绗?'o' 鐨勭涓€娆″嚭鐜颁綅缃紝骞惰緭鍑虹粨鏋溿€?/p>
鍦ㄧ粰瀹氱殑绀轰緥瀛楃涓?"hello world" 涓紝瀛楃 'o' 绗竴娆″嚭鐜板湪浣嶇疆 4锛堜粠闆跺紑濮嬭鏁帮級锛屽洜姝よ緭鍑虹粨鏋滀负 "Found at position: 4"銆?/p>
娉ㄦ剰浜嬮」锛?/strong>
strchr 鍑芥暟鍙繑鍥炵涓€涓尮閰嶉」鐨勬寚閽堬紝濡傛灉闇€瑕佹壘鍒版墍鏈夊尮閰嶉」锛屽彲浠ヤ娇鐢ㄥ惊鐜亶鍘嗗瓧绗︿覆銆?/p>
娉ㄦ剰澶勭悊杩斿洖鍊间负 NULL 鐨勬儏鍐碉紝琛ㄧず鏈壘鍒板尮閰嶉」銆?/p>
1.2 strrchr (String Reverse Character):
鐢ㄦ硶锛?/strong>
strrchr 鍑芥暟鐢ㄤ簬鍦ㄥ瓧绗︿覆涓煡鎵炬寚瀹氬瓧绗︾殑鏈€鍚庝竴涓尮閰嶉」锛屽苟杩斿洖璇ュ瓧绗︽墍鍦ㄤ綅缃殑鎸囬拡銆傚鏋滄湭鎵惧埌鎸囧畾瀛楃锛屽垯杩斿洖 NULL銆?/p>
绀轰緥锛?/strong>
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "hello world";
char *ptr;
ptr = strrchr(str, 'o');
if (ptr != NULL) {
printf("Found at position: %ld\n", ptr - str);
} else {
printf("Not found\n");
}
return 0;
}
缁撴灉锛?/strong>
Found at position: 7
浠g爜鍒嗘瀽锛?/strong>
杩欐C浠g爜棣栧厛瀹氫箟浜嗕竴涓瓧绗︽暟缁?str 鍖呭惈瀛楃涓?"hello world"銆?/p>
鐒跺悗锛岄€氳繃璋冪敤 strrchr 鍑芥暟锛屽湪瀛楃涓蹭腑浠庡彸寰€宸︽煡鎵惧瓧绗?'o' 鐨勬渶鍚庝竴娆″嚭鐜扮殑浣嶇疆锛屽苟灏嗚浣嶇疆鐨勬寚閽堣祴鍊肩粰 ptr 鍙橀噺銆?/p>
鎺ョ潃锛岄€氳繃鏉′欢鍒ゆ柇妫€鏌?ptr 鏄惁涓?NULL锛屽鏋滀笉鏄紝鍒欒〃绀烘壘鍒颁簡瀛楃 'o'锛屽苟浣跨敤 printf 鍑芥暟杈撳嚭鍏跺湪瀛楃涓蹭腑鐨勪綅缃紙閫氳繃鎸囬拡鍑忔硶璁$畻鐩稿浣嶇疆锛夛紝鍚﹀垯杈撳嚭 "Not found"銆?/p>
鏈€鍚庯紝绋嬪簭杩斿洖0锛岃〃绀烘甯告墽琛岀粨鏉熴€?/p>
杩欐浠g爜鐨勪綔鐢ㄦ槸鍦ㄥ瓧绗︿覆涓煡鎵惧瓧绗?'o' 鐨勬渶鍚庝竴娆″嚭鐜颁綅缃紝骞惰緭鍑虹粨鏋溿€?/p>
鍦ㄧ粰瀹氱殑绀轰緥瀛楃涓?"hello world" 涓紝瀛楃 'o' 鏈€鍚庝竴娆″嚭鐜板湪浣嶇疆 7锛堜粠闆跺紑濮嬭鏁帮級锛屽洜姝よ緭鍑虹粨鏋滀负 "Found at position: 7"銆?/p>
娉ㄦ剰浜嬮」锛?/strong>
strrchr 鍑芥暟浠庡瓧绗︿覆鐨勬湯灏惧紑濮嬫悳绱紝鎵惧埌绗竴涓尮閰嶉」鍚庡嵆鍋滄鎼滅储锛岃繑鍥炶鍖归厤椤圭殑鎸囬拡銆?/p>
澶勭悊杩斿洖鍊间负 NULL 鐨勬儏鍐碉紝琛ㄧず鏈壘鍒板尮閰嶉」銆?/p>
1.3 strstr (String Search):
鐢ㄦ硶锛?/strong>
strstr 鍑芥暟鐢ㄤ簬鍦ㄥ瓧绗︿覆涓煡鎵炬寚瀹氬瓙瀛楃涓茬殑绗竴涓尮閰嶉」锛屽苟杩斿洖璇ュ瓙瀛楃涓茬殑鎸囬拡銆傚鏋滄湭鎵惧埌瀛愬瓧绗︿覆锛屽垯杩斿洖 NULL銆?/p>
绀轰緥锛?/strong>
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "hello world";
char *ptr;
ptr = strstr(str, "lo");
if (ptr != NULL) {
printf("Found at position: %ld\n", ptr - str);
} else {
printf("Not found\n");
}
return 0;
}
缁撴灉锛?/strong>
Found at position: 3
浠g爜鍒嗘瀽锛?/strong>
杩欐C浠g爜棣栧厛瀹氫箟浜嗕竴涓瓧绗︽暟缁?str 鍖呭惈瀛楃涓?"hello world"銆?/p>
鐒跺悗锛岄€氳繃璋冪敤 strstr 鍑芥暟锛屽湪瀛楃涓蹭腑鏌ユ壘瀛愬瓧绗︿覆 "lo" 鐨勭涓€娆″嚭鐜扮殑浣嶇疆锛屽苟灏嗚浣嶇疆鐨勬寚閽堣祴鍊肩粰 ptr 鍙橀噺銆?/p>
鎺ョ潃锛岄€氳繃鏉′欢鍒ゆ柇妫€鏌?ptr 鏄惁涓?NULL锛屽鏋滀笉鏄紝鍒欒〃绀烘壘鍒颁簡瀛愬瓧绗︿覆 "lo"锛屽苟浣跨敤 printf 鍑芥暟杈撳嚭鍏跺湪瀛楃涓蹭腑鐨勪綅缃紙閫氳繃鎸囬拡鍑忔硶璁$畻鐩稿浣嶇疆锛夛紝鍚﹀垯杈撳嚭 "Not found"銆?/p>
鏈€鍚庯紝绋嬪簭杩斿洖0锛岃〃绀烘甯告墽琛岀粨鏉熴€?/p>
杩欐浠g爜鐨勪綔鐢ㄦ槸鍦ㄥ瓧绗︿覆涓煡鎵惧瓙瀛楃涓?"lo" 鐨勭涓€娆″嚭鐜颁綅缃紝骞惰緭鍑虹粨鏋溿€?/p>
鍦ㄧ粰瀹氱殑绀轰緥瀛楃涓?"hello world" 涓紝瀛愬瓧绗︿覆 "lo" 绗竴娆″嚭鐜板湪浣嶇疆 3锛堜粠闆跺紑濮嬭鏁帮級锛屽洜姝よ緭鍑虹粨鏋滀负 "Found at position: 3"銆?br> 娉ㄦ剰浜嬮」锛?/strong>
strstr 鍑芥暟浼氫粠瀛楃涓蹭腑鎵惧埌绗竴涓尮閰嶅瓙瀛楃涓茬殑浣嶇疆锛屽苟杩斿洖璇ュ瓙瀛楃涓茬殑鎸囬拡銆?/p>
澶勭悊杩斿洖鍊间负 NULL 鐨勬儏鍐碉紝琛ㄧず鏈壘鍒板尮閰嶉」銆?/p>
杩欎簺鍑芥暟鏄湪C璇█涓父鐢ㄧ殑瀛楃涓叉悳绱㈠伐鍏凤紝鍙互鏍规嵁鍏蜂綋闇€姹傞€夋嫨鍚堥€傜殑鍑芥暟鏉ュ疄鐜板瓧绗︿覆鎼滅储鍔熻兘銆?/p>
浜屻€佸瓧绗︿覆闀垮害鍑芥暟
褰撳湪C璇█涓渶瑕佽幏鍙栧瓧绗︿覆鐨勯暱搴︽椂锛屽彲浠ヤ娇鐢?strlen 鍑芥暟銆備笅闈㈠皢璇︾粏浠嬬粛 strlen 鍑芥暟鐨勭敤閫斻€佺敤娉曘€佺ず渚嬩互鍙婃敞鎰忎簨椤广€?/p>
2.1 strlen (String Length):
鐢ㄩ€旓細
strlen 鍑芥暟鐢ㄤ簬璁$畻瀛楃涓茬殑闀垮害锛屽嵆瀛楃涓蹭腑鐨勫瓧绗︿釜鏁帮紝涓嶅寘鎷瓧绗︿覆缁撴潫绗︼紙'\0'锛夈€?/p>
鐢ㄦ硶锛?/strong>
鍑芥暟鍘熷瀷濡備笅锛?/p>
size_t strlen(const char *str);
鍙傛暟 str锛氳璁$畻闀垮害鐨勫瓧绗︿覆銆?/p>
杩斿洖鍊硷細杩斿洖瀛楃涓茬殑闀垮害銆?/p>
绀轰緥锛?/strong>
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "hello world";
size_t length;
length = strlen(str);
printf("Length of the string: %zu\n", length);
return 0;
}
缁撴灉锛?/strong>
Length of the string: 11
浠g爜鍒嗘瀽锛?/strong>
杩欐C浠g爜棣栧厛瀹氫箟浜嗕竴涓瓧绗︽暟缁?str 鍖呭惈瀛楃涓?"hello world"銆?/p>
鐒跺悗锛岄€氳繃璋冪敤 strlen 鍑芥暟鑾峰彇瀛楃涓茬殑闀垮害锛屽苟灏嗙粨鏋滆祴鍊肩粰 length 鍙橀噺銆?/p>
鎺ョ潃锛岄€氳繃浣跨敤 printf 鍑芥暟杈撳嚭瀛楃涓茬殑闀垮害锛屾牸寮忓寲瀛楃涓蹭腑 %zu 鏄敤浜庢墦鍗?size_t 绫诲瀷鐨勯暱搴︺€?/p>
鏈€鍚庯紝绋嬪簭杩斿洖0锛岃〃绀烘甯告墽琛岀粨鏉熴€?/p>
杩欐浠g爜鐨勪富瑕佸姛鑳芥槸璁$畻骞惰緭鍑哄瓧绗︿覆 "hello world" 鐨勯暱搴︺€?/p>
鍦ㄧ粰瀹氱殑绀轰緥涓紝瀛楃涓茬殑闀垮害涓?1锛屽洜姝よ緭鍑虹粨鏋滀负 "Length of the string: 11"銆?/p>
娉ㄦ剰浜嬮」锛?/strong>
strlen 杩斿洖鐨勬槸 size_t 绫诲瀷鐨勬棤绗﹀彿鏁存暟锛屽洜姝や娇鐢?%zu 鏍煎紡璇存槑绗︽墦鍗扮粨鏋溿€?/p>
娉ㄦ剰瀛楃涓蹭腑鐨勭粨鏉熺 \0 涓嶈鍏ヨ绠楅暱搴︼紝鍗宠繑鍥炲€间负瀛楃涓蹭腑瀛楃鐨勫疄闄呬釜鏁般€?/p>
杩欐槸涓€涓畝鍗曡€屽父鐢ㄧ殑瀛楃涓叉搷浣滃嚱鏁帮紝鐢ㄤ簬鑾峰彇瀛楃涓茬殑闀垮害銆傚湪浣跨敤鏃讹紝闇€瑕佹敞鎰忓瓧绗︿覆鐨勭粨灏惧繀椤绘槸绌哄瓧绗?\0銆傚鏋滃瓧绗︿覆娌℃湁姝g‘浠ョ┖瀛楃缁撳熬锛宻trlen 鍙兘浼氬鑷存湭瀹氫箟鐨勮涓恒€?/p>
鍥犳锛屽湪浣跨敤 strlen 鏃讹紝纭繚瀛楃涓叉槸浠ョ┖瀛楃姝g‘缁堟鐨勩€?/p>
涓夈€佸瓧绗︿覆鍒嗗壊鍑芥暟
strtok 鍑芥暟鐢ㄤ簬鍦–璇█涓垎鍓插瓧绗︿覆銆傚畠鍏佽浣犲皢涓€涓瓧绗︿覆鎷嗗垎鎴愬涓瓙瀛楃涓诧紝鏍规嵁鎸囧畾鐨勫垎闅旂杩涜鍒嗗壊銆?br> 涓嬮潰灏嗚缁嗕粙缁?strtok 鍑芥暟鐨勭敤閫斻€佺敤娉曘€佺ず渚嬩互鍙婃敞鎰忎簨椤广€?/p>
3.1 strtok (String Tokenizer):
鐢ㄩ€旓細
strtok 鐢ㄤ簬灏嗗瓧绗︿覆鎷嗗垎鎴愪竴绯诲垪鐨勬爣璁帮紙tokens锛夛紝杩欎簺鏍囪鐢辩敤鎴锋寚瀹氱殑鍒嗛殧绗︽潵瀹氫箟銆?/p>
姣忔璋冪敤 strtok锛屽畠杩斿洖涓嬩竴涓爣璁帮紝鐩村埌瀛楃涓茬粨鏉熸垨鑰呭啀娌℃湁鍏朵粬鏍囪涓烘銆?/p>
鐢ㄦ硶锛?/strong>
鍑芥暟鍘熷瀷濡備笅锛?/p>
char *strtok(char *str, const char *delimiters);
鍙傛暟 str锛氳鍒嗗壊鐨勫瓧绗︿覆锛岀涓€娆¤皟鐢ㄦ椂浼犲叆寰呭垎鍓茬殑瀛楃涓诧紝鍚庣画璋冪敤浼犲叆 NULL銆?/p>
鍙傛暟 delimiters锛氬垎闅旂瀛楃涓诧紝鍖呭惈鎵€鏈夊彲鑳界殑鍒嗛殧绗︺€?/p>
杩斿洖鍊硷細杩斿洖涓嬩竴涓爣璁扮殑鎸囬拡锛屽鏋滄病鏈夋洿澶氭爣璁板垯杩斿洖 NULL銆?/p>
绀轰緥锛?/strong>
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "apple,orange,banana";
const char delimiters[] = ",";
// 绗竴娆¤皟鐢?
char *token = strtok(str, delimiters);
while (token != NULL) {
printf("Token: %s\n", token);
// 鍚庣画璋冪敤锛屼紶鍏ULL琛ㄧず缁х画浣跨敤鍚屼竴瀛楃涓?
token = strtok(NULL, delimiters);
}
return 0;
}
缁撴灉锛?/strong>
Token: apple
Token: orange
Token: banana
浠g爜鍒嗘瀽锛?/strong>
杩欐C浠g爜棣栧厛瀹氫箟浜嗕竴涓瓧绗︽暟缁?str 鍖呭惈瀛楃涓?"apple,orange,banana"锛屼互鍙婁竴涓寘鍚垎闅旂 "," 鐨勫瓧绗︽暟缁?delimiters銆?/p>
鐒跺悗锛岄€氳繃绗竴娆¤皟鐢?strtok 鍑芥暟锛屽皢瀛楃涓?str 鎸夌収鍒嗛殧绗?"," 杩涜鍒嗗壊锛屽苟杩斿洖绗竴涓垎鍓插悗鐨勫瓙瀛楃涓茬殑鎸囬拡锛屽苟灏嗚鎸囬拡璧嬪€肩粰 token 鍙橀噺銆?/p>
鎺ョ潃锛岄€氳繃涓€涓?while 寰幆锛屼笉鏂鏌?token 鏄惁涓?NULL锛屽鏋滀笉鏄紝鍒欒〃绀轰粛鏈夋湭澶勭悊鐨勫瓙瀛楃涓诧紝渚濇杈撳嚭姣忎釜瀛愬瓧绗︿覆锛屽苟閫氳繃鍚庣画璋冪敤 strtok(NULL, delimiters)
缁х画鑾峰彇涓嬩竴涓垎鍓插悗鐨勫瓙瀛楃涓诧紝鐩村埌鎵€鏈夊瓙瀛楃涓查兘琚鐞嗗畬姣曘€?/p>
鏈€鍚庯紝绋嬪簭杩斿洖0锛岃〃绀烘甯告墽琛岀粨鏉熴€?/p>
杩欐浠g爜鐨勪綔鐢ㄦ槸灏嗗瓧绗︿覆 "apple,orange,banana" 鎸夌収閫楀彿杩涜鍒嗗壊锛屽苟杈撳嚭姣忎釜鍒嗗壊鍚庣殑瀛愬瓧绗︿覆銆?/p>
杈撳嚭缁撴灉涓烘瘡涓瓙瀛楃涓诧紙apple銆乷range銆乥anana锛夊垎鍒崰涓€琛屻€?/p>
娉ㄦ剰浜嬮」锛?/strong>
strtok 鍦ㄥ鐞嗗瓧绗︿覆鏃朵細淇敼鍘熷瀛楃涓诧紝灏嗗垎闅旂鏇挎崲涓?\0锛屽洜姝ら渶瑕佽皑鎱庝娇鐢紝鐗瑰埆鏄綋鍘熷瀛楃涓叉槸甯搁噺瀛楃涓叉椂銆?/p>
鍦ㄥ绾跨▼鐜涓嬶紝strtok 鏄笉鍙噸鍏ョ殑锛屽洜涓哄畠浣跨敤浜嗛潤鎬佸彉閲忔潵璁板綍褰撳墠鐨勮В鏋愪綅缃€?/p>
濡傛灉瀛楃涓蹭腑鏈夎繛缁殑鍒嗛殧绗︼紝strtok 浼氬皢瀹冧滑瑙嗕负涓€涓垎闅旂澶勭悊锛屽洜姝ゅ彲鑳借繑鍥炵┖鏍囪
鍥?鎬荤粨锛?/strong>
鏈枃浠嬬粛浜咰璇█涓殑涓€浜涘父瑙佸瓧绗︿覆鍑芥暟锛屽寘鎷瓧绗︿覆鎼滅储鍑芥暟銆佸瓧绗︿覆闀垮害鍑芥暟鍜屽瓧绗︿覆鍒嗗壊鍑芥暟銆?/p>
閫氳繃瀛︿範杩欎簺鍑芥暟锛屾垜浠彲浠ュ湪C璇█涓洿鍔犵伒娲诲拰楂樻晥鍦板鐞嗗瓧绗︿覆鏁版嵁銆?/p>
瀛楃涓叉悳绱㈠嚱鏁?strchr銆乻trrchr 鍜?strstr 鍙互甯姪鎴戜滑鍦ㄥ瓧绗︿覆涓煡鎵剧壒瀹氬瓧绗︽垨瀛愬瓧绗︿覆鐨勪綅缃€?/p>
瀛楃涓查暱搴﹀嚱鏁?strlen 鑳藉鍑嗙‘鍦拌绠楀瓧绗︿覆鐨勯暱搴︼紝涓哄瓧绗︿覆鎿嶄綔鎻愪緵浜嗗熀纭€銆?/p>
瀛楃涓插垎鍓插嚱鏁?strtok 鍒欒兘澶熷皢瀛楃涓叉寜鐓ф寚瀹氱殑鍒嗛殧绗﹁繘琛屽垎鍓诧紝鎻愬彇鍑哄叾涓殑瀛愬瓧绗︿覆銆?/p>
閫氳繃鎺屾彙杩欎簺瀛楃涓插嚱鏁扮殑浣跨敤鏂规硶锛屾垜浠彲浠ユ洿鍔犻珮鏁堝湴缂栧啓C璇█绋嬪簭锛屽苟澶勭悊鍚勭瀛楃涓叉搷浣滈渶姹傘€?/p>
杩欑瘒鏂囩珷鍒拌繖灏辩粨鏉熷暒
濡傛灉瑙夊緱杩欑瘒鍗氬瀵逛綘鏈夌敤鐨勮瘽锛屽埆蹇樿涓夎繛鍝︺€?/strong>
鎴戞槸鐢滅編鐨勬睙锛岃鎴戜滑鎴戜滑涓嬫鍐嶈
鏈枃鐢卞崥瀹竴鏂囧鍙戝钩鍙?OpenWrite 鍙戝竷锛?/p>