C语言 写一个把一个字符串分成若干个数组 c语言 将一个字符串数组分成几个数组

c\u8bed\u8a00\u600e\u4e48\u628a\u4e00\u4e2a\u5b57\u7b26\u6570\u7ec4\u5206\u6210\u591a\u4e2a\u6570\u7ec4\uff1f

\u53ef\u4ee5\u4f7f\u7528strtok\u51fd\u6570\u628a\u4e00\u4e2a\u5b57\u7b26\u6570\u7ec4\u5206\u89e3\u6210\u591a\u4e2a\u5b57\u7b26\u6570\u7ec4\u3002
1\u3001strtok\u51fd\u6570\uff1a\u539f\u578b\uff1achar *strtok(char s[], const char *delim)\uff1b\u529f\u80fd\uff1a\u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u5206\u89e3\u4e3a\u4e00\u7ec4\u5b57\u7b26\u4e32\uff0cs\u4e3a\u8981\u5206\u89e3\u7684\u5b57\u7b26\u4e32\uff0cdelim\u4e3a\u5206\u9694\u7b26\u5b57\u7b26\u4e32\uff1b\u8bf4\u660e\uff1a\u5f53strtok\u51fd\u6570\u5728\u53c2\u6570s\u7684\u5b57\u7b26\u4e32\u4e2d\u53d1\u73b0\u53c2\u6570delim\u4e2d\u5305\u542b\u7684\u5206\u5272\u5b57\u7b26\u65f6,\u5219\u4f1a\u5c06\u8be5\u5b57\u7b26\u6539\u4e3a\0 \u5b57\u7b26\u3002\u5728\u7b2c\u4e00\u6b21\u8c03\u7528\u65f6\uff0cstrtok()\u5fc5\u9700\u7ed9\u4e88\u53c2\u6570s\u5b57\u7b26\u4e32\uff0c\u5f80\u540e\u7684\u8c03\u7528\u5219\u5c06\u53c2\u6570s\u8bbe\u7f6e\u6210NULL\u3002\u6bcf\u6b21\u8c03\u7528\u6210\u529f\u5219\u8fd4\u56de\u6307\u5411\u88ab\u5206\u5272\u51fa\u7247\u6bb5\u7684\u6307\u9488\uff1b\u5934\u6587\u4ef6\uff1astring.h\uff1b\u8fd4\u56de\u503c\uff1a\u4ece\u5b57\u7b26\u4e32s\u5f00\u5934\u5f00\u59cb\u7684\u4e00\u4e2a\u4e2a\u88ab\u5206\u5272\u7684\u5b57\u7b26\u4e32\u3002\u5206\u5272\u7ed3\u675f\u65f6\uff0c\u8fd4\u56deNULL\u3002\u6240\u6709delim\u4e2d\u5305\u542b\u7684\u5b57\u7b26\u90fd\u4f1a\u88ab\u6ee4\u6389\uff0c\u5e76\u5c06\u88ab\u6ee4\u6389\u7684\u5730\u65b9\u8bbe\u4e3a\u4e00\u5904\u5206\u5272\u7684\u8282\u70b9\u30022\u3001\u4f8b\u7a0b\uff1a
#include#includeint main(){ char input[16]="abc,d,ef,g";//\u5f85\u5206\u89e3\u5b57\u7b26\u4e32 char* delim=",";//\u5206\u9694\u7b26\u5b57\u7b26\u4e32 char* p=strtok(input,delim);//\u7b2c\u4e00\u6b21\u8c03\u7528strtok while(p!=NULL){//\u5f53\u8fd4\u56de\u503c\u4e0d\u4e3aNULL\u65f6\uff0c\u7ee7\u7eed\u5faa\u73af printf("%s\n",p);//\u8f93\u51fa\u5206\u89e3\u7684\u5b57\u7b26\u4e32 p=strtok(NULL,delim);//\u7ee7\u7eed\u8c03\u7528strtok\uff0c\u5206\u89e3\u5269\u4e0b\u7684\u5b57\u7b26\u4e32 } return 0;}

str\u662f\u4e2a\u5b57\u7b26\u4e32\u6570\u7ec4\u7684\u5427\uff0c\u4e0d\u80fd\u6309\u7167lz\u7684\u90a3\u79cd\u5b9a\u4e49\u65b9\u6cd5\u3002
\u53ef\u4ee5\uff1achar *str[]={"2000/01/02,3.5,5.5",
"2001/01/02,2.5,6.6"}
\u60a8\u8c8c\u4f3c\u662f\u8981\u5c06\u65e5\u671f\u548c\u540e\u9762\u7684\u5206\u5f00\uff0c\u8fd9\u6837\u7684\u8bdd','(\u9017\u53f7)\u5c31\u662f\u6807\u5fd7\uff0c\u53ef\u4ee5\u4f5c\u4e3a\u4f60\u7684\u5faa\u73af\u7684\u6761\u4ef6\u3002
int counter=0;
int i=0;
while(i<MAX)
{
counter=0;
while(counter\uff01=',')
{//\u590d\u5236}
while(str[i][counter]!='\0')
{//\u590d\u5236}
i++;
}

void * Split(const  char * pString ,int length)
{
 char * ptr=NULL;
 int rows;//一组等宽字符串可以看做二维数组的一行,定义行数
 const char *pSrc=pString;//取原地址作为源指针
 char *pTag;//目标指针
 //分割长度小于等于0,或指针无效时,返回空指针。
 if (pString && length>0 )
 {
  int len=strlen(pString);
  int cols;//列数
  rows=len/length;//字符串总长除以列数
  if (len%length>0)rows++;//如果余数非0,则行数加一
  ptr=new char[rows*(length+1)];//创建足够的空间
  pTag=ptr;//初始化目标指针
  while (*pSrc!='\0')//源指针指向的值如果有效则循环
  {
   cols=strlen(pSrc);//取原指针开始的字符串长度
   if (cols>length)cols=length;//如果大于分割长度则修正,否则即为余数,就是最后一行的列数
   memcpy(pTag,pSrc,cols);//复制
   pTag+=length;//目标指针递增一个列宽(行宽度)
   *pTag='\0';//填写结束符
   pTag++;//增补一个地址
   pSrc+=cols;//源指针递增一个列宽(行宽度)
  }
 }
 return ptr;
}

//主函数 
int _tmain(int argc, _TCHAR* argv[])
{
 char *str="abcdefghijklmn";
 char (*p)[4];
 p=(char (*)[4])Split(str,3);
 cout<<p[1]<<endl;
getchar();
 return 0;
}


#include <stdio.h>
#include <string.h>
#include <malloc.h>

char **lengthSplit(const char *s, int n,int *m) {
int i,j;
char **lens;
if(strlen(s)%n == 0) *m = strlen(s)/n;
else *m = strlen(s)/n + 1;
    lens = (char **)malloc(*m);
for(i = 0; i < *m; i++)
lens[i] = (char *)malloc(n + 1); 
    for(i = 0; i < *m; ++i) {
for(j = 0; j < n; ++j) 
lens[i][j] = s[j + i * n];
lens[i][j] = '\0';
    }
return lens;
}

int main() {
int i,len,n = 4;
char s[] = "abcdefghijklmnopqrstuvwxyz";
char **strs = lengthSplit(s,n,&len);
for(i = 0; i < len; ++i)
printf("%s
",strs[i]);
    return 0;
}


#include "string.h"
#include "conio.h" 
#include <malloc.h>
int a;
char **Split(const char *s, int n){

if (strlen(s) % n == 0){
a = strlen(s) / n;
}
else  {
a = strlen(s) / n + 1;
}
char **lens;
lens = (char**)malloc(sizeof(char)* a);
int i, j;
for (i = 0; i<a; i++){
lens[i] = (char*)malloc(sizeof(char)* n);
}
for (i = 0; i < a; i++){
for (j = 0; j < n; j++){
lens[i][j] = s[j + i*n];


}
lens[i][n] = '\0';//
}

/*for (i = 0; i < a; i++)
free(lens[i]);
free(lens);
*/
return lens;
}
void main()
{
int i, j,n;
char s[30];
printf("输入
");
scanf("%s", &s);
printf("
");
char(**p);
p = Split(s, 5);
for (i = 0; i < a; i++)
     printf("%s
", p[i]);

_getch();
}


c
语言编写
#include
void
main()
{char
*str[]={"2000/01/02,3.5,5.5","2001/01/02,2.5,6.6"};//当要保存几个字符串是要定义指针数组*str[]来存放每个字符串的首地址
像char
str[]={"i
love
china"}
char
*str1[]
;double
str2[][2];//是两行两列的
char
*p1,*p2
,*p3;
int
i,j=0;
p1=str[0];
p2=str[1];
while(*p1!=",")
p1++
*p1="\0";
while(*p2!=",")
p1++
*p2="\0";
for(i=0;i<=1;i++)
str1[j++]=str[i];
p3=str2;
p3=p1;
p3++=p2;
for(i=0;i<1;i++)
printf("%s\n",*(str1+i));
for(i=0;i<1;i++)
for(j=0;j<1;j++)
printf("%d",str2[i][j]);
}
}

free(lens);

return lens;

扩展阅读:c语言printf输出格式 ... c 如何比较字符串的大小 ... c语言怎么输出中文字符 ... c语言 字符串长度 ... c#截取字符串中的一部分 ... 输入一个字符串c语言 ... c #根据符号分割字符串 ... 怎样输入一个字符串 ... c语言取字符串中的一段 ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网