從指定字串裡提需要的資料

2021-07-03 08:18:09 字數 1326 閱讀 6794

#include 

#include 

//函式作用:從指定字串裡提取你要的資料

//引數說明:

//psourc:源串指標

//pheadertail:起始和結尾標識字元,這裡只能包含兩個字元

//presult:存放結果資料的指標

int extractdata(const char *psourc, const char *pheadertail, char *presult)

const char *ptmp = psourc;

int ipointerindex = 0;

int ifindheader = 0;

int ifindtail = 0;

int istartindex = 0;

int iendindex = 0;

while(*ptmp != null)

}}//如果沒有找到結束標記,查詢結束標記

if (!ifindtail)

}} ++ipointerindex;

++ptmp;

}//起始和結束標記都找到,拷貝資料

if (ifindheader && ifindtail)

//否則,直接返回失敗

return -1;

}//以16進製制形式輸出資料

void outputhex(const char *pdata)

printf("\n");

}int main()

;//三組測試資料,注意要以\0結尾

char psource = ;

char psource1 = ;

char psource2 = ;

//標記字串,兩個字元

char pheadertail = ; 

//開始測試

printf("data is:\n");

outputhex(psource);

if(0 == extractdata(psource, pheadertail, cresult))

else

printf("data is:\n");

outputhex(psource1);

if(0 == extractdata(psource1, pheadertail, cresult))

else

printf("data is:\n");

outputhex(psource2);

if(0 == extractdata(psource2, pheadertail, cresult))

else

return 0;

}結果截圖:

從源字串查詢指定字串的實現

從源字串查詢目標字串 resourcecharline 源字串 targetline 目標字串 返回目標字串在源字串中第一次出現的位置 找不到返回 1 int findcharline const char reourcecharline,const char targetline 相等的話 els...

python從字串中提取指定內容

我們在做資料處理的時候,會遇到包含多條格式類似的長字串,比如說 recv node1 temperature 26 node1 humidity 48 node2 temperature 36 node2 humidity 48 node3 temperature 24 node3 humidity...

從字串的指定位置刪除指定長度的子串

題目 從字串的指定位置刪除指定長度的子串。例如,乙個字串為 abcdefg 指定從第三個字元開始刪除長度為3的子串。刪除後字串變為 abfg 分析 1 直接略過要刪除的字元,使用strcpy將後面的拷前來。2 手動計算下標位置,乙個字元乙個字元拷貝 從第n個字元開始,刪除連續len個字元 char ...