c语言如何分割字符串并将结果放入数组? c语言如何把一串字符分成几部分存入到不同数组中?
c\u8bed\u8a00\u5207\u5272\u5b57\u7b26\u4e32\u5b58\u5165\u6570\u7ec4\uff1fchar b[]={0xaa,0xbb,0xcc,0xdd,0xee,0xff}; \u9996\u5148\u4f60\u8981\u77e5\u9053\u4f60\u8fd9\u4e48\u58f0\u660e\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c0xaa\u800c\u4e0d\u662f\u201c0xaa\u201d\u90a3\u4e48b\u4e2d\u7684\u5143\u7d20\u5c31\u4f1a\u81ea\u52a8\u6839\u636e\u5341\u516d\u8fdb\u5236\u7684\u503c\u8f6c\u6362\u6210\u5b57\u7b26\uff0c\u5c31\u5f88\u6709\u53ef\u80fd\u4e71\u7801\u4e86\u3002
\u8981\u4e48\u4f60\u60f3\u5f97\u5230\u7684\u662fchar b[][6]={"0xaa","0xbb","0xcc","0xdd","0xee","0xff"};
\u8981\u4e48\u5c31\u662fint b[]={0xaa,0xbb,0xcc,0xdd,0xee,0xff}; \u4f60\u4e0a\u9762\u60f3\u5f97\u5230\u7684\u5b57\u7b26\u6570\u7ec4\u662f\u4e0d\u5bf9\u7684\u3002
\u5982\u679c\u662f\u7b2c\u4e00\u4e2a\uff0c\u90a3\u5f88\u7b80\u5355
\u5199\u4e2a\u5faa\u73af\uff0c\u5206\u5272\u4e4b\u540e\u52a0\u4e0a0x\u5c31\u884c\u4e86\u3002
\u5982\u679c\u60f3\u5f97\u5230\u7684\u662f\u7b2c\u4e8c\u4e2a
\u90a3\u4e48\u5c31\u7528\u7b2c\u4e00\u4e2a\u7684\u7ed3\u679c\u4e2d\u7684\u5b57\u7b26\u4e32\u5143\u7d20\u8f6c\u6362\u6210int\u5c31\u53ef\u4ee5\u4e86\u3002
\u6027\u8d28\u90fd\u4e00\u6837\uff0c\u5728c\u8bed\u8a00\u4e2d\u5b57\u7b26\u6570\u7ec4\u5c31\u662f\u5b57\u7b26\u4e32\u3002strcpy\u548cstrncpy\u7528\u9014\u662f\u4e00\u6837\u7684\uff0c\u540e\u8005\u9650\u5236\u4e86\u62f7\u8d1d\u5b57\u7b26\u7684\u957f\u5ea6\uff0c\u8d85\u8fc7\u957f\u5ea6\u5c06\u4e22\u5f03\u4e0d\u62f7\u8d1d\u3002\u6bd4\u5982\u6709a1[5],a2[]="123456",strncpy(a1,a2,5),\u540ea1\u5c31\u662f"12345"\u4e86\uff0c\u56e0\u4e3a\u9650\u5236\u4e86\u62f7\u8d1d5\u4e2a\u957f\u5ea6\uff0c\u540e\u9762\u591a\u51fa\u7684\u5c31\u88ab\u4e22\u5f03\u4e86\u3002\u800cstrcpy\u505a\u4e0d\u5230\u8fd9\u4e00\u70b9\uff0c\u5b83\u4f1a\u6ca1\u6709\u9650\u5236\u5730\u62f7\u8d1d\uff0c\u5982\u679c\u8d85\u51fa\u6570\u7ec4\u6240\u80fd\u5bb9\u7eb3\u7684\u957f\u5ea6\uff0c\u591a\u4f59\u7684\u5c31\u4f1a\u6ea2\u51fa\u5230\u5185\u5b58\u4e2d\u3002\u6240\u4ee5\u8bf4strncpy\u662fstrcpy\u7684\u5b89\u5168\u6a21\u5f0f\u3002
如果是从输入读,那不用存字符串,直接存数组就好了比如
scanf(“%d,%d....”,a,a+1,a+2...)
如果已经是字符串了,比如str,那么用sscanf
比如sscanf(str,“%d,%d....”,a,a+1,a+2...)
调用scanf函数读入字符串
遍历字符串的每一个字符,记录字符串中数字的个数
根据数字的个数调用malloc函数创建动态数组a
再次遍历字符串并将数字字符减去'0',把得到的值填入数组a
绛旓細濡傛灉鏄楀彿鍒嗛殧鐨锛屽彲浠ョ敤涓嬮潰鏂规硶杩涜鍒囧壊锛歩nclude <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( ...
绛旓細include<string.h> int main(void){ char str[1000];//瀹氫箟涓涓瀛楃涓鏁扮粍 char strnew[1000];//瀹氫箟涓涓鐢ㄥ瓧绗︿覆鏁扮粍 char m[] = " ";//瀹氫箟绌烘牸鍙橀噺 printf("璇疯緭鍏ヤ竴涓插瓧绗锛");//鏂囧瓧鎻愮ず杈撳叆瀛楃涓 gets(str);//杈撳叆瀛楃涓 char *p = strtok(str,m);//鍙杝tr涓巑鐨勬寚閽 print...
绛旓細鎶婂瓧绗︿覆瀛樻斁鍒版暟缁勯噷闈,涓涓竴涓瘮瀵癸紙寰幆锛夊悗杈撳嚭 鍒ゆ柇鏁板瓧锛歠or (i=0;i<n;i++){ if ((p[i]>='0') && (p[i]<='9'))printf();鍒ゆ柇瀛楁瘝锛歠or (i=0;i<n;i++){ if ((p[i]>='a') && (p[i]<='Z'))printf();...
绛旓細濡傛灉鏄粠杈撳叆璇伙紝閭d笉鐢ㄥ瓨瀛楃涓诧紝鐩存帴瀛樻暟缁勫氨濂戒簡 姣斿 scanf锛堚%d锛%d...鈥濓紝a锛宎+1锛宎+2...锛夊鏋滃凡缁忔槸瀛楃涓蹭簡锛屾瘮濡俿tr锛岄偅涔堢敤sscanf 姣斿sscanf锛坰tr锛屸%d锛%d...鈥濓紝a锛宎+1锛宎+2...锛
绛旓細涓嶉渶瑕佷笓闂ㄥ垎鍓锛孋璇█閲岄潰鏈潵灏辨槸鐢ㄥ瓧绗︽暟缁勬潵淇濆瓨鐨勶紝濡傦細char a[20]="Hello world!";杩欎釜瀛楃涓诧紝char[0]灏辨槸H锛宑har[1]灏辨槸e銆傚鏋滆鍒嗗壊瀛愪覆锛屽彲浠ヤ娇鐢╯trtok鍑芥暟銆俢har *strtok(char *s, char *delim);鍒嗚В瀛楃涓蹭负涓缁勫瓧绗︿覆銆俿涓鸿鍒嗚В鐨勫瓧绗︿覆锛宒elim涓哄垎闅旂瀛楃涓层傞娆¤皟鐢ㄦ椂锛宻...
绛旓細鍒╃敤C璇█鏍囧噯搴撲腑鐨剆trtok锛堬級鍑芥暟锛屽彲浠ヨ交鏉惧揩鎹风殑灏嗕互绌烘牸鍒嗛殧寮鐨勪竴涓插瓧绗鍒嗘垚澶氫釜瀛愪覆锛屽瓨鏀惧埌瀛楃鎸囬拡鏁扮粍涓傚嚱鏁拌鏄庯細include <string.h> //澶存枃浠 char *strtok(char *str, const char *delim);strtok()鐢ㄦ潵灏嗗瓧绗︿覆鍒嗗壊鎴愪竴涓釜鐗囨銆傚弬鏁皊tr鎸囧悜娆插垎鍓茬殑瀛楃涓诧紝鍙傛暟delim鍒欎负鍒嗗壊瀛楃涓...
绛旓細1銆乻trtok鍑芥暟锛氬師鍨嬶細char *strtok(char s[], const char *delim)锛涘姛鑳斤細灏嗕竴涓瀛楃涓鍒嗚В涓轰竴缁勫瓧绗︿覆锛宻涓鸿鍒嗚В鐨勫瓧绗︿覆锛宒elim涓哄垎闅旂瀛楃涓诧紱璇存槑锛氬綋strtok鍑芥暟鍦ㄥ弬鏁皊鐨勫瓧绗︿覆涓彂鐜板弬鏁癲elim涓寘鍚殑鍒嗗壊瀛楃鏃,鍒欎細灏嗚瀛楃鏀逛负\0 瀛楃銆傚湪绗竴娆¤皟鐢ㄦ椂锛宻trtok()蹇呴渶缁欎簣鍙傛暟s瀛楃涓...
绛旓細缁欏嚭浠g爜: #include <iostream>#include <cstdio>#include <cstring>#include <cctype>using namespace std;int main(){ int num,snum; num=0; snum=0; char s[100]; char zi[100]; char fu[100]; scanf("%s",s); for(int i=0;i<strlen(s);i++) ...
绛旓細浠巗寮澶村紑濮嬬殑涓涓釜琚鍒嗗壊鐨勪覆銆傚綋s涓殑瀛楃鏌ユ壘鍒版湯灏炬椂锛岃繑鍥濶ULL銆傚鏋滄煡鎵句笉鍒癲elim涓殑瀛楃鏃讹紝杩斿洖褰撳墠strtok鐨瀛楃涓鐨勬寚閽堛傛敞鎰忥細strtok宸ヤ綔鍘熺悊涓猴紝鍦ㄥ瓧绗︿覆s涓煡鎵惧瓧绗︿覆delim鍖呭惈鐨勫瓧绗︼紝鑻ユ壘鍒颁竴涓紝鍒欏皢璇ヤ綅缃殑瀛楃璁句负'\0'锛屽苟杩斿洖s銆傛瘡娆¤皟鐢╯trtok閮藉彧鑳借繑鍥1涓瓙涓诧紝鎵浠ヨ嫢瑕佸娆...
绛旓細0xaa","0xbb","0xcc","0xdd","0xee","0xff"};瑕佷箞灏辨槸int b[]={0xaa,0xbb,0xcc,0xdd,0xee,0xff}; 浣犱笂闈㈡兂寰楀埌鐨勫瓧绗︽暟缁勬槸涓嶅鐨勩傚鏋滄槸绗竴涓紝閭e緢绠鍗 鍐欎釜寰幆锛鍒嗗壊涔嬪悗鍔犱笂0x灏辫浜嗐傚鏋滄兂寰楀埌鐨勬槸绗簩涓 閭d箞灏辩敤绗竴涓殑缁撴灉涓殑瀛楃涓鍏冪礌杞崲鎴恑nt灏卞彲浠ヤ簡銆