C语言怎么把字符串按行分割 C语言分割字符串

C\u8bed\u8a00\u5982\u4f55\u5b9e\u73b0\u5206\u5272\u5b57\u7b26\u4e32\u4e3a\u4e00\u4e2a\u4e2a\u5355\u72ec\u7684\u5b57\u7b26\u5e76\u4fdd\u5b58

\u4e0d\u9700\u8981\u4e13\u95e8\u5206\u5272\uff0cC\u8bed\u8a00\u91cc\u9762\u672c\u6765\u5c31\u662f\u7528\u5b57\u7b26\u6570\u7ec4\u6765\u4fdd\u5b58\u7684\uff0c\u5982\uff1achar a[20]="Hello world!";\u8fd9\u4e2a\u5b57\u7b26\u4e32\uff0cchar[0]\u5c31\u662fH\uff0cchar[1]\u5c31\u662fe\u3002
\u5982\u679c\u8981\u5206\u5272\u5b50\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528strtok\u51fd\u6570\u3002
char *strtok(char *s, char *delim);\u5206\u89e3\u5b57\u7b26\u4e32\u4e3a\u4e00\u7ec4\u5b57\u7b26\u4e32\u3002s\u4e3a\u8981\u5206\u89e3\u7684\u5b57\u7b26\u4e32\uff0cdelim\u4e3a\u5206\u9694\u7b26\u5b57\u7b26\u4e32\u3002\u9996\u6b21\u8c03\u7528\u65f6\uff0cs\u6307\u5411\u8981\u5206\u89e3\u7684\u5b57\u7b26\u4e32\uff0c\u4e4b\u540e\u518d\u6b21\u8c03\u7528\u8981\u628as\u8bbe\u6210NULL\u3002strtok\u5728s\u4e2d\u67e5\u627e\u5305\u542b\u5728delim\u4e2d\u7684\u5b57\u7b26\u5e76\u7528NULL('')\u6765\u66ff\u6362\uff0c\u76f4\u5230\u627e\u904d\u6574\u4e2a\u5b57\u7b26\u4e32\u3002

\u4f7f\u7528strtok\u51fd\u6570\u5373\u53ef\u5b9e\u73b0\u5206\u5272\u5b57\u7b26\u4e32\u3002
1\u3001strtok\u51fd\u6570\uff1a\u539f\u578b\uff1achar *strtok(char s[], const char *delim)\uff1b\u529f\u80fd\uff1a\u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u5206\u89e3\u4e3a\u4e00\u7ec4\u5b57\u7b26\u4e32\uff0cs\u4e3a\u8981\u5206\u89e3\u7684\u5b57\u7b26\u4e32\uff0cdelim\u4e3a\u5206\u9694\u7b26\u5b57\u7b26\u4e32\uff1b\u8bf4\u660e\uff1a\u5f53strtok\u51fd\u6570\u5728\u53c2\u6570s\u7684\u5b57\u7b26\u4e32\u4e2d\u53d1\u73b0\u53c2\u6570delim\u4e2d\u5305\u542b\u7684\u5206\u5272\u5b57\u7b26\u65f6,\u5219\u4f1a\u5c06\u8be5\u5b57\u7b26\u6539\u4e3a\0 \u5b57\u7b26\u3002\u5728\u7b2c\u4e00\u6b21\u8c03\u7528\u65f6\uff0cstrtok()\u5fc5\u9700\u7ed9\u4e88\u53c2\u6570s\u5b57\u7b26\u4e32\uff0c\u5f80\u540e\u7684\u8c03\u7528\u5219\u5c06\u53c2\u6570s\u8bbe\u7f6e\u6210NULL\u3002\u6bcf\u6b21\u8c03\u7528\u6210\u529f\u5219\u8fd4\u56de\u6307\u5411\u88ab\u5206\u5272\u51fa\u7247\u6bb5\u7684\u6307\u9488\uff1b\u5934\u6587\u4ef6\uff1astring.h\uff1b\u8fd4\u56de\u503c\uff1a\u4ece\u5b57\u7b26\u4e32s\u5f00\u5934\u5f00\u59cb\u7684\u4e00\u4e2a\u4e2a\u88ab\u5206\u5272\u7684\u5b57\u7b26\u4e32\u3002\u5206\u5272\u7ed3\u675f\u65f6\uff0c\u8fd4\u56deNULL\u3002\u6240\u6709delim\u4e2d\u5305\u542b\u7684\u5b57\u7b26\u90fd\u4f1a\u88ab\u6ee4\u6389\uff0c\u5e76\u5c06\u88ab\u6ee4\u6389\u7684\u5730\u65b9\u8bbe\u4e3a\u4e00\u5904\u5206\u5272\u7684\u8282\u70b9\u30022\u3001\u4f8b\u7a0b\uff1a
#include#includeint main(){ char input[16]="abc,d,ef,g";//\u5f85\u5206\u89e3\u5b57\u7b26\u4e32 char* delim=",";//\u5206\u9694\u7b26\u5b57\u7b26\u4e32 char* p=strtok(input,delim);//\u7b2c\u4e00\u6b21\u8c03\u7528strtok while(p!=NULL){//\u5f53\u8fd4\u56de\u503c\u4e0d\u4e3aNULL\u65f6\uff0c\u7ee7\u7eed\u5faa\u73af printf("%s\n",p);//\u8f93\u51fa\u5206\u89e3\u7684\u5b57\u7b26\u4e32 p=strtok(NULL,delim);//\u7ee7\u7eed\u8c03\u7528strtok\uff0c\u5206\u89e3\u5269\u4e0b\u7684\u5b57\u7b26\u4e32 } return 0;}

int split(char dst[][80], char* str, const char* spl)

{

int n = 0;

char *result = NULL;

result = strtok(str, spl);

while( result != NULL )

{

strcpy(dst[n++], result);

result = strtok(NULL, spl);

}

return n;

}

int _tmain(int argc, _TCHAR* argv[])

{

char str[] = "123,456
789,321";

char dst[10][80];

int cnt = split(dst, str, "
");

for (int i = 0; i < cnt; i++)

puts(dst[i]);

return 0;

}

主要是字符串分割函数strtok的使用



  • C璇█涓瓧绗﹀垏鍓鍑芥暟split鐨勫疄鐜
    绛旓細include <stdio.h>#include <string.h>// 灏唖tr瀛楃浠pl鍒嗗壊,瀛樹簬dst涓紝骞惰繑鍥炲瓙瀛楃涓鏁伴噺int split(char dst[][80], char* str, const char* spl){ int n = 0; char *result = NULL; result = strtok(str, spl); while( result != NULL ) { strcpy(dst[n+...
  • 鐢C璇█鍒嗗壊瀛楃涓
    绛旓細while(result_switchs != NULL) { printf( "result_switchs is \"%s\"\n",result_switchs);char* tmp = result_switchs; ---鍔犱竴琛岋紝tmp锛鍒嗗壊鍚庣殑绗竴涓瀛楃涓 result_switchs = strtok(NULL,delims_switchs); result_switchs 宸茬粡绛変簬鍒嗗壊鍚庣殑绗簩涓瓧绗︿覆浜 char swinfo[100];str...
  • C璇█浠ラ楀彿鍒嗗壊瀛楃涓
    绛旓細include<iostream> include<vector> include<sstream> usingnamespacestd;intmain(){ strings;vector<int>v;cin>>s;//灏嗚鍏ョ殑瀛楃涓杞寲鎴恑s娴 istringstreamis(s);intinter;charch;while(is>>inter)//鍙兘璇诲嚭is娴佷腑鐨勪竴涓暣褰㈣杩沬nter { v.push_back(inter);is>>ch;//鐒跺悗璇讳竴涓瓧绗﹀瀷璇...
  • c璇█濡備綍鍒嗗壊瀛楃涓骞灏缁撴灉鏀惧叆鏁扮粍?
    绛旓細濡傛灉鏄粠杈撳叆璇伙紝閭d笉鐢ㄥ瓨瀛楃涓锛岀洿鎺ュ瓨鏁扮粍灏卞ソ浜 姣斿 scanf锛堚%d锛%d...鈥濓紝a锛宎+1锛宎+2...锛夊鏋滃凡缁忔槸瀛楃涓蹭簡锛屾瘮濡俿tr锛岄偅涔堢敤sscanf 姣斿sscanf锛坰tr锛屸%d锛%d...鈥濓紝a锛宎+1锛宎+2...锛
  • C璇█瀛楃涓缁勬垚鍜鍒嗗壊闂
    绛旓細缁勬垚瀛楃涓鐨勫姙娉曞緢绠鍗曪細char buf[100];sprintf(buf, "%d,%d,%d,%d,%d", a, b, c, d, e);杩欐牱buf鏁扮粍涓氨瀛樺緱鏄"10,20,30,40,50"杩欎釜瀛楃涓蹭簡銆傛兂鍒嗗紑鐨勮瘽锛屽畾涔変竴涓寚閽坧锛岃杩欎釜鎸囬拡浠巄uf[0] 寮濮嬫寚锛岃p ++锛屾瘡娆$洿鍒伴楀彿鐨勬椂鍊欏氨璺冲嚭锛屽鏋滀笉鏄楀彿鐨勮瘽锛屽氨灏嗗綋鍓嶆寚閽堟寚鍚戠殑...
  • c璇█涓瓧绗︿覆鍒嗗壊闂,鍦ㄧ嚎鎬ョ瓑銆傘傘
    绛旓細瑕佽揪鍒拌繖鏍风殑鏁堟灉锛岀敤sscanf鐪嬫潵鏄悶涓嶅畾浜嗭紝鍙兘鑷繁鍐欏嚱鏁般備笅闈㈢殑鍑芥暟鐪嬫槸鍚︽弧瓒抽渶姹傦紝浣犲彲浠ュ湪鍏ㄥ眬鍙橀噺str[]閲屽姞浠绘剰澶氱殑璺緞锛屼絾鏈変竴鐐规敞鎰忕殑鏄紝瀛楃\鍦ㄤ覆閲屽繀椤昏浆涔変负\\锛岃矾寰勯暱搴︿笉瓒呰繃128浣嶅嵆鍙俰nclude <stdio.h> include <string.h> include <stdlib.h> char str[] = "clamscan c:\...
  • c璇█涓庝箞鍒嗚鏄剧ず瀛楃涓鍛傘傘
    绛旓細int i=0;while(buf[i]!=鈥淺0鈥濓級{ putchar(buf[i]);i++;if(i>=2){ i=0;putchar("\n");} }
  • c璇█鑷繁缂栧啓涓涓猻plit瀛楃涓插垎鍓鍑芥暟
    绛旓細char p[]="ab,cde,efgh,ghi32";char c=',';int i=0,j=0,m=0;while(p[i]){ if(p[i]==',')j++;i++;} j=j+1;//鎺ュ彈鍒板簳鏈夊灏戜釜鍗曡瘝鍦鍒嗗壊鍚庨氳繃閫楀彿鍘诲垽鏂 k=j;char**q=split(p,c,k);printf("\n");for(i=0;i<4;i++){ cout<<q[i];cout<<endl;} } ...
  • C璇█ 鍐欎竴涓妸涓涓瀛楃涓鍒嗘垚鑻ュ共涓暟缁
    绛旓細='\0')//婧愭寚閽堟寚鍚戠殑鍊煎鏋滄湁鏁堝垯寰幆 { cols=strlen(pSrc);//鍙栧師鎸囬拡寮濮嬬殑瀛楃涓闀垮害 if (cols>length)cols=length;//濡傛灉澶т簬鍒嗗壊闀垮害鍒欎慨姝o紝鍚﹀垯鍗充负浣欐暟锛屽氨鏄渶鍚庝竴琛岀殑鍒楁暟 memcpy(pTag,pSrc,cols);//澶嶅埗 pTag+=length;//鐩爣鎸囬拡閫掑涓涓垪瀹斤紙琛屽搴︼級 *pTag=...
  • C璇█涓杈撳叆瀛楃涓,閲岄潰鏈夌┖鏍,鎬庝箞鏍规嵁绌烘牸鎶婂瓧绗︿覆鍒嗗紑,骞跺瓨鍦ㄦ暟缁勯噷...
    绛旓細绋嬪簭婧愮爜濡備笅锛歩nclude<stdio.h> include<string.h> int main(void){ char str[1000];//瀹氫箟涓涓瀛楃涓鏁扮粍 char strnew[1000];//瀹氫箟涓涓鐢ㄥ瓧绗︿覆鏁扮粍 char m[] = " ";//瀹氫箟绌烘牸鍙橀噺 printf("璇疯緭鍏ヤ竴涓插瓧绗锛");//鏂囧瓧鎻愮ず杈撳叆瀛楃涓 gets(str);//杈撳叆瀛楃涓 char *p = strtok(...
  • 扩展阅读:c语言字符串截取分割 ... c语言如何分割字符串 ... c++如何分割char字符串 ... c语言读一行字符串 ... c++分割string字符串 ... c语言实现字符串分割 ... c#字符串截取方法 ... c#分割字符串的方法 ... c语言怎么输入多行字符串 ...

    本站交流只代表网友个人观点,与本站立场无关
    欢迎反馈与建议,请联系电邮
    2024© 车视网