这是题目:
用字符串操作实现一个字符串分割Split的程序,要求:
- 输入一个用分号“;”做分隔符的字符串;
- 将初始字符串从每个分号“;”处分割,形成一个字符串数组;
- 要求将分割出来的子串存入一个块链结构存储的字符串数组中;
- 按照字符串数组顺序输出分割结果,如下示例所示:
初始字符串:”abc;edf;cde;xyz”
字符串1:”abc;”
字符串2:”edf;”
字符串2:”cde;”
字符串2:”xyz;”
#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;
}
这是我写的代码,可以编译,但是运行不了,不知道出了什么,大家可以编译一下看有什么问题。