#include <stdio.h>
#include <string.h>
#include<stdlib.h>
int a = 0;
int l = 0;
int main()
{
char copu [10][100];
char str[100];
char ooo[100];
printf_s("请输入你想分割的字符串,且用分号分割开\n");
puts(str);
int sz = 0, fh = 0, fhq = 0; //sz是分组,fh是有几个分号,fhq是分号之间的数目
int n = 0;
while (str[n]!='\0') //当str不为0的时候
{
if (str[n] == ';') //出现分号时 n为分号的数字
{
if (fh == 0) //出现第一个分号
{
fhq = n; //fhq=分号前的数字
memcpy(ooo, str, fhq+1); //复制str前的fhq的数给ooo数组
ooo[fhq + 1] = '\0'; //令ooo数组分号之后的那个数为休止符
for (int e = 0;e < fhq + 1;e++)
{
copu[sz][e] = ooo[e];
};
sz++; //多了一组
n++; //n变大
fh++; //分号+1'
a = n;
}
else
{
int i = n;
int p = n - fhq;
memcpy(ooo, str + fhq + 1, p + 1);
for (int e = 0;e < p + 1;e++)
{
copu[sz][e] = ooo[e];
};
sz++;
n++;
fh++;
a = n;
}
}
n++;
if (str[n] == '\0') //到最后出现\0的时候,首先要计算分号之间的个数,然后将+1变成;,+2变成\0
{
int w = n - a;
int z = n - fhq;
memcpy(ooo, str + a + 1, w + 1);
for (int e = 0;e < w + 1;e++)
{
copu[sz][e] = ooo[e];
};
copu[fh][w-1] = ';';
copu[fh][w + 2] = ';';
l = fh;
n++;
fh++;
}
}
for (int s = 0;s < l-1;s++)
{
printf("%s\n", copu[s]);
};
return 0;
}
这是我写的代码,可以编译,但是运行不了,不知道出了什么,大家可以bian'yi
修改如下,见注释,供参考:
#include <stdio.h>
#include <string.h>
#include<stdlib.h>
//int a = 0;
//int l = 0;
int main()
{
char copu [10][100];
char str[100];
char ooo[100];
printf("请输入你想分割的字符串,且用分号分割开\n");
//puts(str);
gets(str);
int sz = 0, fh = 0, fhq = 0; //sz是分组,fh是有几个分号,fhq是分号之间的数目
int n = 0;
while (str[n]!='\0') //当str不为0的时候
{
if (str[n] == ';') //出现分号时 n为分号的数字
{
if (fh == 0) //出现第一个分号
{
fhq = n; //fhq=分号前的数字
memcpy(ooo,str,fhq);//memcpy(ooo, str, fhq+1); 复制str前的fhq的数给ooo数组
ooo[fhq] = '\0'; // ooo[fhq + 1] = '\0'; 令ooo数组分号之后的那个数为休止符
for (int e = 0;e < fhq + 1 ;e++)
{
copu[sz][e] = ooo[e];
} //;
sz++; //多了一组
//n++; n变大
fh++; //分号+1'
//a = n;
}
else
{
//int i = n;
int p = n - fhq - 1; // int p = n - fhq;
memcpy(ooo,str + fhq + 1,p); // memcpy(ooo, str + fhq + 1, p + 1);
ooo[p] = '\0';
fhq = n;
for (int e = 0;e < p+1 ;e++)
{
copu[sz][e] = ooo[e];
} //;
sz++;
//n++;
fh++;
//a = n;
}
}
n++;
if (str[n] == '\0') //到最后出现\0的时候,首先要计算分号之间的个数,然后将+1变成;,+2变成\0
{
//int w = n - a;
int z = n - fhq - 1;
memcpy(ooo, str + fhq + 1, z);
ooo[z] = '\0';
for (int e = 0;e < z + 1;e++)
{
copu[sz][e] = ooo[e];
} //;
sz++;
fh++;
//copu[fh][w-1] = ';';
//copu[fh][w + 2] = ';';
//l = fh;
//n++;
//fh++;
}
}
for (int s = 0;s < sz;s++)
{
printf("%s\n", copu[s]);
} //;
return 0;
}
/*
请输入你想分割的字符串,且用分号分割开
123; jxsd ;sfadfsa;098
123
jxsd
sfadfsa
098
请按任意键继续. . .
*/