当前位置:首页 » 编程语言 » c语言字符串拆分

c语言字符串拆分

发布时间: 2025-09-25 02:04:39

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;
}
大体思路,没有编译,上班仓促写了个框架,哈哈

热点内容
java返回this 发布:2025-10-20 08:28:16 浏览:710
制作脚本网站 发布:2025-10-20 08:17:34 浏览:972
python中的init方法 发布:2025-10-20 08:17:33 浏览:681
图案密码什么意思 发布:2025-10-20 08:16:56 浏览:833
怎么清理微信视频缓存 发布:2025-10-20 08:12:37 浏览:741
c语言编译器怎么看执行过程 发布:2025-10-20 08:00:32 浏览:1081
邮箱如何填写发信服务器 发布:2025-10-20 07:45:27 浏览:312
shell脚本入门案例 发布:2025-10-20 07:44:45 浏览:192
怎么上传照片浏览上传 发布:2025-10-20 07:44:03 浏览:879
python股票数据获取 发布:2025-10-20 07:39:44 浏览:837