c如何按行切割字符串
A. C语言中字符切割函数split的实现
#include<stdio.h>
#include<string.h>
//将抄str字符以spl分割袭,存于dst中,并返回子字符串数量
intsplit(chardst[][80],char*str,constchar*spl)
{
intn=0;
char*result=NULL;
result=strtok(str,spl);
while(result!=NULL)
{
strcpy(dst[n++],result);
result=strtok(NULL,spl);
}
returnn;
}
intmain()
{
charstr[]="whatisyouname?";
chardst[10][80];
intcnt=split(dst,str,"");
for(inti=0;i<cnt;i++)
puts(dst[i]);
return0;
}
B. c/cpp中如何分割字符串,类似于split的功能
c语言中 有切割字符串的函数啊!
strtok函数(const char *str, const char *ch)
第一个是字符串,第内二个是以那个字符做切容割。
例子:
#include <stdio.h>
#include <string.h>
int main(void)
{
char s[] = "123-sldkf-123ls-343434-dfjdlkfj-dflcmvn";
char *delim = "-";
char *p;
printf("%s ", strtok(s, delim));
while((p = strtok(NULL, delim))
printf("%s ", p);
printf("\n");
return 0;
}
char s[] = "dlkjf-sdlkfjsdf\lsdkljfl'sldkjf\sdkfjsdlf\lskdjfls";
如果想用-和\两个符号切割的话
char *delim = "-\";
这样就可以了
C. 用C语言分割字符串
char 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; ---------加一行,tmp=分割后的第一个字符串
result_switchs = strtok(NULL,delims_switchs); result_switchs 已经等于分割后的第二个字符串了
char swinfo[100];
strcpy(swinfo,tmp); ------------------换成tmp
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);
}
}
D. C语言如何以逗号分割字符串 就是用scanf输入一行“1,2,3,4"把这四个数据存到数组该怎么写
int a[4];
scanf("%d,%d,%d,%d", &a[0], &a[1], &a[2], &a[3]);
E. C 中怎么把字符串按固定长度分割
#include<stdio.h>
#include<string.h>
intmain(void)
{
chars[100]="1122334455667788 ";
charp[10][10];
char*q=s,tmp;
inti=0,len=strlen(s),intk=4;
while(strlen(q)>k)
{
tmp=q[k];
q[k]='