C语言字符串组成和分割问题
\u7528C\u8bed\u8a00\u5206\u5272\u5b57\u7b26\u4e32char szPortsMessage[]="0,1,4,6,7;-1062708991|0,3,4,5,7;-1062708992|";
char delims_switchs[] = "|";
char *result_switchs = NULL;
result_switchs = strtok(szPortsMessage,delims_switchs);
while(result_switchs != NULL) {
printf( "result_switchs is \"%s\"\n",result_switchs);
char* tmp = result_switchs; ---------\u52a0\u4e00\u884c\uff0ctmp\uff1d\u5206\u5272\u540e\u7684\u7b2c\u4e00\u4e2a\u5b57\u7b26\u4e32
result_switchs = strtok(NULL,delims_switchs); result_switchs \u5df2\u7ecf\u7b49\u4e8e\u5206\u5272\u540e\u7684\u7b2c\u4e8c\u4e2a\u5b57\u7b26\u4e32\u4e86
char swinfo[100];
strcpy(swinfo,tmp); \uff0d\uff0d\uff0d\uff0d\uff0d\uff0d\uff0d\uff0d\uff0d\uff0d\uff0d\uff0d\uff0d\uff0d\uff0d\uff0d\uff0d\uff0d\u6362\u6210tmp
char delims_switch[] = ";";
char *result_switch = NULL;
result_switch = strtok(swinfo,delims_switch);
while(result_switch != NULL) {
printf( "result_switch is \"%s\"\n",result_switch);
result_switch = strtok(NULL,delims_switch);
}
}
\u8981\u8fbe\u5230\u8fd9\u6837\u7684\u6548\u679c\uff0c\u7528sscanf\u770b\u6765\u662f\u641e\u4e0d\u5b9a\u4e86\uff0c\u53ea\u80fd\u81ea\u5df1\u5199\u51fd\u6570\u3002
\u4e0b\u9762\u7684\u51fd\u6570\u770b\u662f\u5426\u6ee1\u8db3\u9700\u6c42\uff0c\u4f60\u53ef\u4ee5\u5728\u5168\u5c40\u53d8\u91cfstr[]\u91cc\u52a0\u4efb\u610f\u591a\u7684\u8def\u5f84\uff0c\u4f46\u6709\u4e00\u70b9\u6ce8\u610f\u7684\u662f\uff0c\u5b57\u7b26\\u5728\u4e32\u91cc\u5fc5\u987b\u8f6c\u4e49\u4e3a\\\uff0c\u8def\u5f84\u957f\u5ea6\u4e0d\u8d85\u8fc7128\u4f4d\u5373\u53ef\u3002
#include
#include
#include
char str[] = "clamscan c:\\Documents and Settings d:\\test e:\\a and b f:\\c and d";
void main()
{
int i = 0, j = 0;
char (*pStr)[128] = NULL;
char *s = str;
char *ss = NULL;
while (1)
{
pStr = (char (*)[128])realloc(pStr, (i+1)*(128));
memset(*(pStr+i), 0, 128);
ss = strchr(s, ':');
if (i == 0)
{
if (ss == NULL)
{
strcpy(*(pStr+i), s);
i++;
break;
}
memcpy(*(pStr+i), s, ss-s-2);
s = ss+1;
i++;
continue;
}
if (ss == NULL)
{
strcpy(*(pStr+i), s-2);
i++;
break;
}
memcpy(*(pStr+i), s-2, ss-s);
s = ss+1;
i++;
}
printf("After despare, the strings are:\n");
for (j = 0; j < i; j++)
{
printf("[%s]\n", *(pStr+j));
}
free(pStr);
}
\u8f93\u51fa\u6548\u679c\uff1a
char buf[100];
sprintf(buf, "%d,%d,%d,%d,%d", a, b, c, d, e);
这样buf数组中就存得是"10,20,30,40,50"这个字符串了。
想分开的话,定义一个指针p,让这个指针从buf[0] 开始指,让p ++,每次直到逗号的时候就跳出,如果不是逗号的话,就将当前指针指向的这个值放到另一个字符数组buf1中,最后将buf1转成数字,用atoi函数,这时候会得到这个数1020304050,用这个数对100取余。得到50,然后除以100再对100取余,得到40,同理,就会得到相应的数字了,把这些数字存入相应的变量就行。
不明白继续追问
#include <stdio.h>
int main()
{
int a,b,c,d,e;
int f,g,h,i,k;
char buf[20];
a = 10;
b = 20;
c = 30;
d = 40;
e = 50;
sprintf(buf,"%02d,%02d,%02d,%02d,%02d",a,b,c,d,e);
sscanf(buf,"%d,%d,%d,%d,%d",&f,&g,&h,&i,&k);
getchar();
return 0;
}
#include<sstream>
#include<iostream>
#include<string>
using naemspace std;
int main()
{
int i;
int a[5];
string temp,str;
stringstream ss;
for(i=0;i<5;i++)
{
ss<<a[i];
ss>>temp;
if(i<4)
temp+=',';
str+=temp;
ss.clear();
}//////////////////str=10,20,30,40,50
int b[5];
for(i=0;i<5;i++)
{
ss<<str[2*i];
ss>>b[i];
ss.clear();
}
return 0;
}
完成!!!
其实这道题最好用sscanf,你可以百度一下~
绛旓細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/C++涓殑Split鍑芥暟鏄痵trtok()鍏跺嚱鏁板師鍨嬪涓:char * strtok (char * str, const char * delimiters);鍑芥暟璇存槑 strtok()鐢ㄦ潵灏瀛楃涓插垎鍓鎴愪竴涓釜鐗囨銆傚弬鏁皊tr鎸囧悜娆插垎鍓茬殑瀛楃涓诧紝鍙傛暟delimiters鍒欎负鍒嗗壊瀛楃涓锛屽綋strtok()鍦ㄥ弬鏁皊tr鐨勫瓧绗︿覆涓彂鐜板埌鍙傛暟delimiters鐨勫垎鍓插瓧绗︽椂鍒欎細灏嗚瀛楃鏀逛负'\0...
绛旓細int main(){ char buf[];//buf涓轰綘鐨勫甫绌烘牸鐨瀛楃涓 char arr1[]; //浠ヤ笅涓哄垎鍒敤浠ュ瓨鍌ㄧ殑瀛楃鏁扮粍 char arr2[];...int i = 0;int counts = 1; //寰幆璁℃暟 char*p =& buf[0]; //璇绘寚閽 while(*p!='\0'){ if(' '==*p){ p++;continue;} else { switch(counts)...
绛旓細string.h涓彁渚涗簡涓涓鍒嗗壊瀛楃涓寰楀埌瀛愪覆鐨勫嚱鏁扳斺攕trtok銆傚師鍨嬶細char *strtok(char s[], const char *delim);鍙傛暟锛歴涓洪渶瑕佸垎鍓茬殑瀛楃涓诧紝delim涓哄垎闅斿瓙涓茬殑瀛楃涓层傝繑鍥炲硷細浠巗寮澶村紑濮嬬殑涓涓釜琚垎鍓茬殑涓层傚綋s涓殑瀛楃鏌ユ壘鍒版湯灏炬椂锛岃繑鍥濶ULL銆傚鏋滄煡鎵句笉鍒癲elim涓殑瀛楃鏃讹紝杩斿洖褰撳墠strtok鐨...
绛旓細1.缂栫▼鍦ㄤ竴涓凡鐭瀛楃涓涓煡鎵炬渶闀垮崟璇,鍋囧畾瀛楃涓蹭腑鍙惈鏈夊瓧姣嶅拰绌烘牸,绌烘牸鐢ㄦ潵鍒嗗壊涓嶅悓鍗曡瘝! 鏂规硶涓: //VC++ 6.0 缂栬瘧閫氳繃 #include void main() { char str[]="hello world my name is xun yi cao !!"; char*p=str,*p1; int max=0,count=0,i; while(*p) { count=0; while(*p==' ')p++...
绛旓細濡傛灉鏄粠杈撳叆璇伙紝閭d笉鐢ㄥ瓨瀛楃涓锛岀洿鎺ュ瓨鏁扮粍灏卞ソ浜 姣斿 scanf锛堚%d锛%d...鈥濓紝a锛宎+1锛宎+2...锛夊鏋滃凡缁忔槸瀛楃涓蹭簡锛屾瘮濡俿tr锛岄偅涔堢敤sscanf 姣斿sscanf锛坰tr锛屸%d锛%d...鈥濓紝a锛宎+1锛宎+2...锛
绛旓細鐩存帴鐢ㄦ牸寮忚緭鍏ュ嚱鏁皊canf(); scanf("%s %s",v1,v2);涓や釜%s涔嬮棿鍔犵┖鏍肩锛岃〃绀轰袱涓瀛楃涓浠ョ┖鏍煎垝鍒嗗紑銆傚瓧绗︿覆姣旇緝鍙互鐩存帴璋冪敤鍑芥暟strcmp(a,b);澶存枃浠朵负string.h銆備篃鍙嚜宸卞畾涔夈佺紪鍐欏嚱鏁拌繘琛屾瘮杈
绛旓細4閮ㄥ垎锛屾槸瑕鎷嗗垎鎴恆bc 123456 def 1 杩4涓儴鍒嗭紵 鍒╃敤strtok 鍑芥暟鏉ユ媶鍒嗐備互绌烘牸涓哄垎闅旂鍙 渚嬪 include <stdio.h> include <string.h> int main (){ char str[] ="- This, a sample string.";char * pch;printf ("Splitting string \"%s\" into tokens:\n",str);pch = strtok (...
绛旓細= strtok(NULL, spl);} return n;} int _tmain(int argc, _TCHAR* argv[]){ char str[] = "123,456\n789,321";char dst[10][80];int cnt = split(dst, str, "\n");for (int i = 0; i < cnt; i++)puts(dst[i]);return 0;} 涓昏鏄瀛楃涓插垎鍓鍑芥暟strtok鐨勪娇鐢 ...
绛旓細='\0')//婧愭寚閽堟寚鍚戠殑鍊煎鏋滄湁鏁堝垯寰幆 { cols=strlen(pSrc);//鍙栧師鎸囬拡寮濮嬬殑瀛楃涓闀垮害 if (cols>length)cols=length;//濡傛灉澶т簬鍒嗗壊闀垮害鍒欎慨姝o紝鍚﹀垯鍗充负浣欐暟锛屽氨鏄渶鍚庝竴琛岀殑鍒楁暟 memcpy(pTag,pSrc,cols);//澶嶅埗 pTag+=length;//鐩爣鎸囬拡閫掑涓涓垪瀹斤紙琛屽搴︼級 *pTag=...