拆分的char型数组字符的两个数组 [英] Split array of chars into two arrays of chars
本文介绍了拆分的char型数组字符的两个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想分裂包含由分隔的两个串字符的一个阵列|成char两个一阳。
下面是我的示例code。
无效splitChar(为const char *文字,字符*文本1,字符*文本2)
{
对于(*文字='\\ 0'和;&放大器; *文字='|'!)*文本1 ++ = *文本++;
*文本1 ='\\ 0';
对于(* ++文本='\\ 0';!)*文本2 ++ = *文本;
*文本2 ='\\ 0';
}INT主(INT ARGC,CHAR *的argv [])
{ 字符*文字=星期一|星期二,文本1 [255],文本2 [255];
splitChar(文本,文本1,文本2);
返回0;
}
我有两个问题:
-
如何用C来进一步改善code(例如重写它在1周期)。
-
如何重写这个code在C ++?
解决方案
有关A,使用内部库:
无效splitChar(为const char *文字,字符*文本1,字符*文本2)
{
INT LEN =(和strchr(文字,'|') - 文本)*的sizeof(字符);
函数strncpy(文本1,文本,LEN);
的strcpy(文本2,文本+ LEN + 1);
}
I would like to split one array of char containing two "strings "separated by '|' into two arays of char.
Here is my sample code.
void splitChar(const char *text, char *text1, char *text2)
{
for (;*text!='\0' && *text != '|';) *text1++ = *text++;
*text1 = '\0';
for (;*++text!='\0';) *text2++ = *text;
*text2 = '\0';
}
int main(int argc, char* argv[])
{
char *text = "monday|tuesday", text1[255], text2 [255];
splitChar (text, text1, text2);
return 0;
}
I have two questions:
How to further improve this code in C (for example rewrite it in 1 for cycle).
How to rewrite this code in C++?
解决方案
For A, using internal libraries:
void splitChar(const char *text, char *text1, char *text2)
{
int len = (strchr(text,'|')-text)*sizeof(char);
strncpy(text1, text, len);
strcpy(text2, text+len+1);
}
这篇关于拆分的char型数组字符的两个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文