c语言字符串拆分
㈠ c语言怎么把字符串按行分割
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语言字符串中切割函数strtok的用法
strtok()函数并不像你想的那样可以一次切割字串。需要多次循环,第二次时需要用 p = strtok(NULL, " "); 这样的 形式。
void main()
{ char test1[] = "Hello C World";
char *p;
p = strtok(test1, " ");
while(p)
{
printf("%s\n", p);
p = strtok(NULL, " ");
}
return 0;
}
运行结果:
Hello
C
World
㈢ C语言有没有把字符串拆分为数组的函数
直接用简单的C++
#include<iostream>
#include<string>
#include<vector>
usingnamespacestd;
//把字符串s按照字符串c进行切分得到vector_v
vector<string>split(conststring&s,conststring&c){
vector<string>v;
intpos1=0,pos2;
while((pos2=s.find(c,pos1))!=-1){
v.push_back(s.substr(pos1,pos2-pos1));
pos1=pos2+c.size();
}
if(pos1!=s.length())
v.push_back(s.substr(pos1));
returnv;
}
intmain()
{
stringinput="张三$|男$|济南$|大专学历$|";
vector<string>myArray=split(input,"$|");
for(inti=0;i<myArray.size();i++){
cout<<myArray[i]<<endl;
}
}
/*
张三
男
济南
大专学历
*/
㈣ C语言输入一个字符串,然后分割成三个,规则入内
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)
{
case:1
while((arr1[i++] = *p++)&&(*p!=' '));
break;
case:2
while((arr2[i++] = *p++)&&(*p!=' '));
break;
.
.
.
}
}
counts++;
i= 0;
}
return 0;
}
大体思路,没有编译,上班仓促写了个框架,哈哈
