当前位置:网站首页>C语言自编字符串处理函数——字符串分割、字符串填充等
C语言自编字符串处理函数——字符串分割、字符串填充等
2022-04-23 15:48:00 【MallocLu】
字符串填充使用示例
#include <stdio.h>
#include <stdlib.h>
#include "CStringTools.h"
int main()
{
char cmd[1000];
char cmdList[50][20];
while (1)
{
scanf("%s", cmd);
if (strcmp(cmd, "exit") == 0)
break;
fillWithKeys(cmd, "*#", ' ');
printf("%s\n\n", cmd);
}
return 0;
}
字符串分割使用示例
#include <stdio.h>
#include <stdlib.h>
#include "CStringTools.h"
int main()
{
char cmd[1000];
char cmdList[50][20];
while (1)
{
scanf("%s", cmd);
if (strcmp(cmd, "exit") == 0)
break;
int cnt = split(cmdList, cmd, "*#");
for (int i = 0; i < cnt; ++i)
printf("%d\t\t%s\n", i+1, cmdList[i]);
printf("\n");
}
return 0;
}
工具函数代码
有.h和.c两个文件,将其加入到你的项目中,并且#include “CStringTools.h”,既可以使用其中的功能。
// CStringTools.h文件
#ifndef CSTRINGTOOLS_H_INCLUDED
#define CSTRINGTOOLS_H_INCLUDED
#include <string.h>
#include <malloc.h>
//在str指定字符keys前后填充一个content
void fillWithKeys(char *str, char* keys, char content);
//将字符串str按照分隔符keys分割成strList(上限50个字符串 长度20)
//返回实际分成的字符串的数量
int split(char strList[][20], char *str, char *keys);
#endif // CSTRINGTOOLS_H_INCLUDED
// CStringTools.c文件
#include "CStringTools.h"
void fillWithKeys(char *str, char* keys, char content)
{
char *tmp = (char *)malloc(sizeof(char) * strlen(str));
strcpy(tmp, str);
int index = 0;
for (int i = 0; i < strlen(tmp); ++i)
{
int flag = 0;
for (int j = 0; j < strlen(keys); ++j)
{
if (keys[j] == tmp[i])
{
flag = 1;
break;
}
}
if (flag)
str[index++] = content;
str[index++] = tmp[i];
if (flag)
str[index++] = content;
}
str[index] = '\0';
free(tmp);
}
int split(char strList[][20], char *str, char *keys)
{
int cnt = 0;
str[strlen(str)] = keys[0];
str[strlen(str)] = '\0';
int start = -1;
for (int i = 0; i < strlen(str); ++i)
{
int flag = 0;
for (int j = 0; j < strlen(keys); ++j)
{
if (keys[j] == str[i])
{
flag = 1;
break;
}
}
if(flag)
{
if (i - start > 1)
{
//切割[start+1,i-1]
for (int j = start + 1; j <= i - 1; ++j)
{
strList[cnt][j-start-1] = str[j];
}
strList[cnt][i - start - 1] = '\0';
++cnt;
}
start = i;
}
}
return cnt;
}
版权声明
本文为[MallocLu]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_42283621/article/details/124364562
边栏推荐
- mysql乐观锁解决并发冲突
- 【AI周报】英伟达用AI设计芯片;不完美的Transformer要克服自注意力的理论缺陷
- 单体架构系统重新架构
- MySQL Cluster Mode and application scenario
- 实现缺省页面
- Spark 算子之filter使用
- CVPR 2022 优质论文分享
- Basic concepts of website construction and management
- utils. Deprecated in35 may be cancelled due to upgrade. What should I do
- pgpool-II 4.3 中文手册 - 入门教程
猜你喜欢
Partitionby of spark operator
CVPR 2022 quality paper sharing
MySQL Cluster Mode and application scenario
实现缺省页面
C language --- string + memory function
[AI weekly] NVIDIA designs chips with AI; The imperfect transformer needs to overcome the theoretical defect of self attention
Codejock Suite Pro v20. three
Temporal model: long-term and short-term memory network (LSTM)
Multi level cache usage
Codejock Suite Pro v20.3.0
随机推荐
s16.基于镜像仓库一键安装containerd脚本
Mobile finance (for personal use)
时序模型:长短期记忆网络(LSTM)
One brush 313 sword finger offer 06 Print linked list from end to end (E)
Go语言数组,指针,结构体
Deeply learn the skills of parameter adjustment
Metalife established a strategic partnership with ESTV and appointed its CEO Eric Yoon as a consultant
Codejock Suite Pro v20. three
Mumu, go all the way
Modèle de Cluster MySQL et scénario d'application
MySQL optimistic lock to solve concurrency conflict
fatal error: torch/extension. h: No such file or directory
IronPDF for . NET 2022.4.5455
Basic concepts of website construction and management
为啥禁用外键约束
PHP classes and objects
Upgrade MySQL 5.1 to 5.67
Codejock Suite Pro v20.3.0
shell脚本中的DATE日期计算
Go并发和通道